0

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'

Mark Tolonen
181k26 gold badges184 silver badges279 bronze badges
asked Jul 28, 2022 at 21:03
1
  • print(repr(entry)) should show strings like "totalRecords" - when you iterate a dictionary, you get its keys. Commented Jul 28, 2022 at 21:26

2 Answers 2

2

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
answered Jul 28, 2022 at 21:21
Sign up to request clarification or add additional context in comments.

1 Comment

That’s the problem I am having. This particular dictionary does not appear to have a key. When I pull it up in something like postman the output looks similar to what I posted. Normally their is a main key with everything nested in it. In this case it goes straight to the nested items but does not show me the actual main key to use in my script to get the nested items.
0

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.

answered Jul 28, 2022 at 21:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.