I would like to find out how I can iterate through the Following JSON in Python 3 Specifically I would like to be able to pull out 'id' and 'lname'. My actual JSON file has about 300 entries
{
"datainfos": [{
"DataInfo": [{
"id": 1,
"lname": "Altitude",
"max": 62.79999923706055,
"min": -37.20000076293945,
"numInstances": 1,
"sname": "ALT",
"unit": "m"
}]
},
{
"DataInfo": []
},
{
"DataInfo": [{
"id": 3,
"lname": "Position Error",
"max": 0,
"min": 0,
"numInstances": 1,
"sname": "EPE",
"unit": "m"
}]
},
{
"DataInfo": [{
"id": 4,
"lname": "HDOP",
"max": 0,
"min": 0,
"numInstances": 1,
"sname": "HDOP",
"unit": ""
}]
}
]
}
My code is as below:
import json
f = open('data1.json')
data = json.load(f)
f.close()
for dataitems in data['datainfos']:
print (dataitems['DataInfo'])
Python returns a list, not a dictionary. When I try to use the following code
import json
f = open('data1.json')
data = json.load(f)
f.close()
for dataitems in data['datainfos']:
print (dataitems['DataInfo']['lnames'])
I get the error:
Traceback (most recent call last): File "C:\\Users\\phil\\Documents\\Python Scripts\\UMP-VDR\\Testing Files\\convertjson.py", line 9, in print (dataitems['DataInfo']['lnames']) TypeError: list indices must be integers or slices, not str [Finished in 0.1s]
Use this code because your DataInfo is array.
import json
f = open('data1.json')
data = json.load(f)
f.close()
for dataitems in data['datainfos']:
for datainfo in dataitems['DataInfo']:
print(datainfo['id'], datainfo['lname'])
Or change your json file like this:
{
"datainfos": [
{
"DataInfo":
{
"id": 1,
"lname": "Altitude",
"max": 62.79999923706055,
"min": -37.20000076293945,
"numInstances": 1,
"sname": "ALT",
"unit": "m"
}
},
{
"DataInfo": {}
},
{
"DataInfo":
{
"id": 3,
"lname": "Position Error",
"max": 0,
"min": 0,
"numInstances": 1,
"sname": "EPE",
"unit": "m"
}
},
{
"DataInfo":
{
"id": 4,
"lname": "HDOP",
"max": 0,
"min": 0,
"numInstances": 1,
"sname": "HDOP",
"unit": ""
}
}
]
}
And change your code like this:
import json
f = open('asd.json')
data = json.load(f)
f.close()
for dataitems in data['datainfos']:
if(dataitems['DataInfo']):
print(dataitems['DataInfo']['id'], dataitems['DataInfo']['lname'])
Iterating over a dictionary in python will return the keys by default. You probably want .items()
. But in this case you probably want:
import json
data = json.load(open('file.json'))
out = []
for x in data['datainfos']:
if x['DataInfo']:
out.append((x['DataInfo'][0]['id'], x['DataInfo'][0]['lname']))
print(out)
>>> [(1, 'Altitude'), (3, 'Position Error'), (4, 'HDOP')]
Whatever i understand from your question is that you want id and lname for that particular id. So to do that in simple way with n^2 complexity is as below:
import json
f = open('data.json')
data = json.load(f)
f.close()
for dataitems in data['datainfos']:
for DataInfo in dataitems['DataInfo']:
print(DataInfo['id'],DataInfo['lname'])
This will give you the particular id with lname. And if you want to store it somewhere then add us to that variable.
for x in range(0,len(sample_dict.get('datainfos'))):
if len(sample_dict.get('datainfos')[x]['DataInfo'])==0:
print('Empty list')
else:
print('ID IS {}, NAME IS {}'.format(sample_dict.get('datainfos')[x]['DataInfo'][0]['id'],sample_dict.get('datainfos')[x]['DataInfo'][0]['lname']))
This will iterate and print id and names. Will print empty for no name & ID. Your json is stored as sample_dict
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.