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'
print(repr(entry))
should show strings like "totalRecords" - when you iterate a dictionary, you get its keys. - tdelaney