Attempting to parse and filter some data for an API I am trying to test. There does not appear to be a main key at the beginning of the dict, so when I attempt to loop through the dictionary, I get one of the two errors:
TypeError: string indices must be integers or KeyError, because the key is not being seen.
An example of the dict:
{
"totalRecords": 2,
"_links": {
"self": "www.test.com",
"next": null,
"previous": null
},
"summaryOfServices": [
{
"data1": 12345678,
"data2": 34567891,
"data3": "Training",
"data4": 1,
"data5": 4,
"data6": 37,
"data7": 20
},
{
"data1": 98765432,
"data2": 55555555,
"data3": "Training2",
"data4": 0,
"data5": 0,
"data6": 7809,
"data7": 0
}
]
}
Snippet if the code:
resp = requests.get(url,
authHeaders)
rprint(url)
json_resp = resp.json()
for entry in json_resp:
item_1 = entry["data1"]
item_2 = entry["data2"]
item_3 = entry["data3"]
item_4 = entry["data4"]
item_5 = entry["data5"]
item_6 = entry["data6"]
count =1
rprint(f"Data 1: {item_1}\tData 2: {item_2}\t"
f"Data 3: {item_3}\tData 4: {item_4}\t"
f"\tData 5: {item_5}\tData 6: {item_6}\n")
TypeError: string indices must be integers
or I get
KeyError: 'data1'
CodePudding user response:
Your outer object is a dictionary. It looks like you want to iterate over the summaryOfServices
data, so you have to fetch that key:
import json
json_string = '''{
"totalRecords": 2,
"_links": {
"self": "www.test.com",
"next": null,
"previous": null
},
"summaryOfServices": [
{
"data1": 12345678,
"data2": 34567891,
"data3": "Training",
"data4": 1,
"data5": 4,
"data6": 37,
"data7": 20
},
{
"data1": 98765432,
"data2": 55555555,
"data3": "Training2",
"data4": 0,
"data5": 0,
"data6": 7809,
"data7": 0
}
]
}'''
count = 0
json_resp = json.loads(json_string)
for entry in json_resp['summaryOfServices']:
item_1 = entry["data1"]
item_2 = entry["data2"]
item_3 = entry["data3"]
item_4 = entry["data4"]
item_5 = entry["data5"]
item_6 = entry["data6"]
count =1
print(f"Data 1: {item_1}\tData 2: {item_2}\t"
f"Data 3: {item_3}\tData 4: {item_4}\t"
f"\tData 5: {item_5}\tData 6: {item_6}")
Output:
Data 1: 12345678 Data 2: 34567891 Data 3: Training Data 4: 1 Data 5: 4 Data 6: 37
Data 1: 98765432 Data 2: 55555555 Data 3: Training2 Data 4: 0 Data 5: 0 Data 6: 7809
CodePudding user response:
Are you sure you are iterating over the correct thing?
You may want to modify your code to:
resp = requests.get(url,
authHeaders)
rprint(url)
json_resp = resp.json()
summary_of_services = json_resp["summaryOfServices"]
for entry in summary_of_services:
item_1 = entry["data1"]
item_2 = entry["data2"]
item_3 = entry["data3"]
item_4 = entry["data4"]
item_5 = entry["data5"]
item_6 = entry["data6"]
count =1
rprint(f"Data 1: {item_1}\tData 2: {item_2}\t"
f"Data 3: {item_3}\tData 4: {item_4}\t"
f"\tData 5: {item_5}\tData 6: {item_6}\n")
Then you will want to think about edge cases like can summaryOfServices be None or something that cannot be iterated? What if the http get call fails, etc.