You can put your JSON data in a variable:
data = {"sensors":
{"-KqYN_VeXCh8CZQFRusI":
{"bathroom_temp": 16,
"date": "02/08/2017",
"fridge_level": 8,
"kitchen_temp": 18,
"living_temp": 17,
"power_bathroom": 0,
"power_bathroom_value": 0,
"power_kit_0": 0
},
"-KqYPPffaTpft7B72Ow9":
{"bathroom_temp": 20,
"date": "02/08/2017",
"fridge_level": 19,
"kitchen_temp": 14,
"living_temp": 20,
"power_bathroom": 0,
"power_bathroom_value": 0
},
"-KqYPUld3AOve8hnpnOy":
{"bathroom_temp": 23,
"date": "02/08/2017",
"fridge_level": 40,
"kitchen_temp": 11,
"living_temp": 10,
"power_bathroom": 1,
"power_bathroom_value": 81,
}
}
}
and then use a nested index address for getting the desired parameter:
kitchen_temp = data["sensors"]["-KqYN_VeXCh8CZQFRusI"]["kitchen_temp"]
print(kitchen_temp)
Answer from nima on Stack OverflowPython is not my goto, so sorry if this is a FAQ question. I need to parse json down a few levels and pull out some relevant data to push into a database for analysis. I see examples using both the native json, and ijson, but none that parse more than the first level.
So here is the sample json, munged a bit:
{
"value1": "walterwhite",
"value2": "school teacher",
"services": [
{
"name": "first sub",
"slug": "ovp",
"vendors": [
{
"slug": "wistia",
"name": "Wistia"
}
],
"vendor_count": 1
},
{
"name": "second sub",
"slug": "hosting",
"vendors": [
{
"slug": "013-netvision",
"name": "013 Netvision"
},
{
"slug": "internap",
"name": "Internap Corp."
},
{
"slug": "eurofiber",
"name": "Eurofiber"
},
{
"slug": "register-com--2",
"name": "Register.com"
}
],
"vendor_count": 4
}
]
}i want to return:
a) All "services.name" ie. "first sub"
b) All "vendors.name" (under services) ie. "wistia"
Many thanks!
hey so I know this is kind of weird to point out, but it looks like you're using Azure... so SQL Server
SQL Server has native JSON parsing functions. https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017
You might be able to save yourself a ton of headaches.
Hmm.. I should count this for my json library thanks for your experience
I think the fundamental answer to your quesions is, "look at the type". The Python json conversion table tells you what to expect for each type. Let's load your file and see what we have, according to the Python interpreter:
>>> input = dat.read()
>>> stores = json.loads(input)
>>> type(stores)
<class 'dict'>
>>> type(stores['features'])
<class 'list'>
>>> type( stores['features'][0] )
<class 'dict'>
>>> type( stores['features'][0]['properties'] )
<class 'dict'>
>>> type( stores['features'][0]['properties']['telephone'] )
<class 'str'>
>>> stores['features'][0]['properties']['telephone']
'4166903659'
Every object has a type; every type has methods. Just work your way down the list.
It looks like your data is structured like this:
features:
- geometry:
coordinates:
- float
- float
properties:
addressLine1: str
addressLine2: str
addressLine3: str
addressLine4: str
customAddress: str
driveTodayHours: str
filterType:
- str
- ...
id: str
longDescription: str
name: str
postcode: str
shortDescription: str
subDivision: str
telephone: str
todayHours: str
You want to pull out information from the individual "feature" elements, which appear to be stores, so you're off to a good start with code like your existing logic:
for store in data['features']:
csv_row = process_store(store)
From there you just need to decide what you want to pull out of the info, e.g.:
coord_1 = store['coordinates'][0]
custom_address = store['properties']['customAddress']
...
. Compared to your existing code I think you just didn't notice there is a properties attribute to access beyond the initial features level.