0

I tried to print a list from a JSON object and I got the error:

TypeError: string indices must be integers

The whole JSON object looks like this:

{'status': 0, 
'body': 
{'activities': [
{'steps': 4144, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-10', 'brand': 18, 'is_tracker': False}, 
{'steps': 4962, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-11', 'brand': 18, 'is_tracker': False}, 
{'steps': 4052, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-12', 'brand': 18, 'is_tracker': True}, 
{'steps': 4375, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-13', 'brand': 18, 'is_tracker': True}, 
{'steps': 5705, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-14', 'brand': 18, 'is_tracker': True}, 
{'steps': 5831, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-15', 'brand': 18, 'is_tracker': True}, 
{'steps': 6460, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-16', 'brand': 18, 'is_tracker': True}, 
{'steps': 1853, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-17', 'brand': 18, 'is_tracker': True}, 
{'steps': 4933, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-18', 'brand': 18, 'is_tracker': True}, 
{'steps': 3247, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-19', 'brand': 18, 'is_tracker': True}], 
'more': False, 'offset': 0}}

I tried to print with the following code:

print(json_response2["body"["activities"["steps"][0]["date"]]])

and the error occurred.

What did I do wrong?

4
  • 1
    print(json_response2["body"]["activities"][0]["steps"], json_response2["body"]["activities"][0]["date"]) Commented Jun 22, 2020 at 10:24
  • What do you like to print each "steps" and "date" or just from the first item? Commented Jun 22, 2020 at 10:29
  • @LeoArad i like to know to print the whole, but for a first try, i just wanted to print the first line. To print all the items, i think i would need a foreloop right? Commented Jun 22, 2020 at 12:05
  • @Andrew Yes, you can look at my answer on how to use the for loop to print all "steps" and "date" from each activity. Commented Jun 22, 2020 at 12:24

2 Answers 2

1

You can print the elements of "date" and "steps" like the following

json_response2 = {'status': 0,
'body':
{'activities': [
{'steps': 4144, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-10', 'brand': 18, 'is_tracker': False},
{'steps': 4962, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-11', 'brand': 18, 'is_tracker': False},
{'steps': 4052, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-12', 'brand': 18, 'is_tracker': True},
{'steps': 4375, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-13', 'brand': 18, 'is_tracker': True},
{'steps': 5705, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-14', 'brand': 18, 'is_tracker': True},
{'steps': 5831, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-15', 'brand': 18, 'is_tracker': True},
{'steps': 6460, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-16', 'brand': 18, 'is_tracker': True},
{'steps': 1853, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-17', 'brand': 18, 'is_tracker': True},
{'steps': 4933, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-18', 'brand': 18, 'is_tracker': True},
{'steps': 3247, 'deviceid': None, 'timezone': 'Europe/Copenhagen', 'date': '2020-06-19', 'brand': 18, 'is_tracker': True}],
'more': False, 'offset': 0}}

for i in json_response2["body"]["activities"]:
    print(i["steps"], i["date"])

This will print the "date" and "steps" for each activity.

Sign up to request clarification or add additional context in comments.

Comments

1
print(json_response2["body"]["activities"][0]["steps"])

You should use the print statement like this. This will print the 'steps' data of the first activity.

If you want to print the 'date' data you can use this statement:

print(json_response2["body"]["activities"][0]["date"])

Comments

Your Answer

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.