Andrew
Andrew

Reputation: 49

How to print nested JSON data in python

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?

Upvotes: 0

Views: 2266

Answers (2)

Leo Arad
Leo Arad

Reputation: 4472

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.

Upvotes: 1

Ataberk Gürel
Ataberk Gürel

Reputation: 36

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"])

Upvotes: 1

Related Questions