I have to set up an analytical system of a Json data base(Firebase Realtime Database).
Here's what it looks like:
{
"247" : {
"activity_duration" : 15,
"battery_used" : 0,
"date" : "2017-09-05",
"day" : 247,
"heat_waves" : 3,
"outside_temperature" : 16.64,
"shirt_temperature" : [ 24.883928571428573, 23.660714285714285 ]
},
"262" : {
"activity_duration" : 240,
"battery_used" : 2,
"date" : "2017-09-20",
"day" : 262,
"heat_waves" : 5,
"outside_temperature" : 21.19,
"shirt_temperature" : [ 24.233616504854368, 22.954490291262136 ]
},
"268" : {
"activity_duration" : 260,
"battery_used" : 5,
"date" : "2017-09-26",
"day" : 268,
"heat_waves" : 4,
"outside_temperature" : 16.07,
"shirt_temperature" : [ 18.68695652173913, 17.576630434782608 ]
}
}
To do this, I want to practice calculations in python on my json file like the average of heat_waves.
The problem is that I can not access the nodes without writing them in raw.
data["247"]["heat_waves"] but I want something like data[0]["heat_waves"].When I try:
import json;
data = [ ];
filename = "Database.json";
with open(filename,'r') as json_data:
data = json.load(json_data);
print(json.dumps(data[0]["heat_waves"], indent=4, sort_keys=True));
I have this error message:
print(json.dumps(data[0]["heat_waves"], indent=4, sort_keys=True));
KeyError: 0
So, my final question is:
How can I access these nodes without writing them in raw?
If you dont want to address elements by key but with index then you can do
>>> import json;
>>>
>>> data = [ ];
>>> filename = "Database.json";
>>> with open(filename,'r') as json_data:
... data = json.load(json_data)
>>> data.values()[0]['heat_waves']
5
>>>
Related
Im getting the json data from a file:
"students": [
{
"name" : "ben",
"age" : 15
},
{
"name" : "sam",
"age" : 14
}
]
}
here's my initial code:
def get_names():
students = open('students.json')
data = json.load(students)
I want to get the values of all names
[ben,sam]
you need to extract the names from the students list.
data = {"students": [
{
"name" : "ben",
"age" : 15
},
{
"name" : "sam",
"age" : 14
}
]
}
names = [each_student['name'] for each_student in data['students']]
print(names) #['ben', 'sam']
Try using a list comprehension:
>>> [dct['name'] for dct in data['students']]
['ben', 'sam']
>>>
import json
with open('./students.json', 'r') as students_file:
students_content = json.load(students_file)
print([student['name'] for student in students_content['students']]) # ['ben', 'sam']
JSON's load function from the docs:
Deserialize fp (a .read()-supporting text file or binary file containing a JSON document) to a Python object...
The JSON file in students.json will look like:
{
"students": [
{
"name" : "ben",
"age" : 15
},
{
"name" : "sam",
"age" : 14
}
]
}
The JSON load function can then be used to deserialize this JSON object in the file to a Python dictionary:
import json
# use with context manager to ensure the file closes properly
with open('students.json', 'rb')as students_fp:
data = json.load(students_fp)
print(type(data)) # dict i.e. a Python dictionary
# list comprehension to take the name of each student
names = [student['name'] for student in data['students']]
Where names now contains the desired:
["ben", "sam"]
I have a JSON file that looks like the following (not the whole file)
"route-information" : [
{
"attributes" : {"xmlns" : "http://xml.juniper.net"},
"route-table" : [
{
"comment" : "keepalive",
"table-name" : [
{
"data" : "inet"
}
],
"destination-count" : [
{
"data" : "24324"
}
],
"total-route-count" : [
{
"data" : "47432"
}
],
"active-route-count" : [
{
"data" : "43252"
}
],
"holddown-route-count" : [
{
"data" : "0"
}
],
"hidden-route-count" : [
{
"data" : "1"
}
],
I am trying to access the 'comment' part by using python.
So far I have this:
import json
# read file
with open('route-table.json') as file:
data = json.load(file)
print(data["route-information"]["route-table"]["comment"])
Whenever I run this part I get the following error and I can't seem to fix it.
Traceback (most recent call last):
File "json_test.py", line 7, in <module>
print(data["route-information"]["route-table"]["comment"])
TypeError: list indices must be integers or slices, not str
data["route-information"] is a list, so you can do data["route-information"][0] to access the dict inside, same with data["route-information"][0]["route-table"]:
print(data["route-information"][0]["route-table"][0]["comment"])
If you intend to use data later and are okay with changing it's structure, you can replace the lists with their first elements (assuming they only have one element) so that you won't have to use the [0] notation every time you need to access the dicts:
data["route-information"] = data["route-information"][0]
data["route-information"]["route-table"] = data["route-information"]["route-table"][0]
print(data["route-information"]["route-table"]["comment"])
MrGeek is correct. Just to add more info
[] are for JSON arrays, which are called list in Python
{} are for JSON objects, which are called dict in Python
To get the value we do as follows:
data["route-information"][0]["route-table"][0]["comment"]
data["attributes"]["xmlns"]
Sort of a new python guy here and haven't had much success with the following.
I have a txt file with data formatted as follows:
{
"$type" : "TableInstance",
"$version" : 1,
"Instance" : "InstanceName",
"ColumnAliases" : [ "", "", ],
"ColumnNames" : [ "keyName", "dateName"],
"ColumnData" : [ {
"type" : "ColumnData1",
"Strings" : [key1, key2],]
}, {
"type" : "ColumnData2",
"Strings" : [date1, date2]}]
}
That I would like to read into a dataframe such that it is formatted as:
[ keyName dateName
key1 date1
key2 date1 ]
Is there a simple way to do this?
does this work for you?
dict = {
"$type" : "TableInstance",
"$version" : 1,
"Instance" : "InstanceName",
"ColumnAliases" : [ "", "", ],
"ColumnNames" : [ "keyName", "dateName"],
"ColumnData" : [ {
"type" : "ColumnData1",
"Strings" : ['key1', 'key2']
}, {
"type" : "ColumnData2",
"Strings" : ['date1', 'date2']}]
}
df = pd.DataFrame({dict['ColumnNames'][0]:dict['ColumnData'][0]['Strings'], dict['ColumnNames'][1]:dict['ColumnData'][1]['Strings']})
It looks it that you stored the serialized python object in the file. Hence, you can deserialize the Python object by the help of pickle, then you can parse the object based on your requirements.
import pickle
import pandas as pd
filePath = 'test.txt'
obj = pd.read_pickle(filePath)
#obj = pickle.load(open(filePath, "rb"))
df = pd.DataFrame({obj['ColumnNames'][0]:obj['ColumnData'][0]['Strings'], obj['ColumnNames'][1]:obj['ColumnData'][1]['Strings']})
I have a config.json file, which contains an array of organisations:
config.json
{
"organisations": [
{ "displayName" : "org1", "bucketName" : "org1_bucket" },
{ "displayName" : "org2", "bucketName" : "org2_bucket" },
{ "displayName" : "org3", "bucketName" : "org3_bucket" }
]
}
How can I get an array of all organisation names?
This is what I have tried:
from python_json_config import ConfigBuilder
def read_config():
builder = ConfigBuilder()
org_array = builder.parse_config('config.json')
# return all firstNames in org_array
import json
def read_config():
display_names = []
with open('yourfilename.json', 'r', encoding="utf-8") as file:
orgs = json.load(file)
display_names = [ o["displayName"] for o in orgs["organizations"] ]
return display_names
Also, we don't have any way to know what happens with ConfigBuilder or builder.parse_config since we don't have access to that code, so sorry to not take into account your example
a = {
"organisations": [
{ "displayName" : "org1", "bucketName" : "org1_bucket" },
{ "displayName" : "org2", "bucketName" : "org2_bucket" },
{ "displayName" : "org3", "bucketName" : "org3_bucket" }
]
}
print([i["displayName"] for i in a["organisations"]])
Output:
['org1', 'org2', 'org3']
Use list comprehension, it's very easy. In order to read a json file.
import json
data = json.load(open("config.json"))
Use lambda with map to get array of only organizations names
>>> list(map(lambda i:i['displayName'],x['organisations']))
>>> ['org1', 'org2', 'org3']
If you want to read json data from file into dictionary you can achieve this as following.
import json
with open('config.json') as json_file:
data = json.load(json_file)
org_array = list(map(lambda i:i['displayName'],data['organisations']))
I got my commuting report from google map timeline and it is in json format. I use these codes:
with open('Location History.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
pd.DataFrame(data)
the dataframe has only one 'location' column.
{"locations" : [ {
"timestampMs" : "1501812184856",
"latitudeE7" : 390632197,
"longitudeE7" : -771227158,
"accuracy" : 10,
"velocity" : 1,
"heading" : 226,
"altitude" : 146,
"verticalAccuracy" : 12
}, {
"timestampMs" : "1501813902831",
"latitudeE7" : 390624516,
"longitudeE7" : -771212199,
"accuracy" : 10,
"velocity" : 5,
"heading" : 316,
"altitude" : 126,
"verticalAccuracy" : 16
},
any advice how I can read the file into multiple columns and one row for each member of dict.
extract 'location' from initial json, and then convert to DataFrame
with open('Location History.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
pd.DataFrame(data['locations'])