Python Accessing Nested JSON Data

I did not realize that the first nested element is actually an array. The correct way access to the post code key is as follows:

r = requests.get('http://api.zippopotam.us/us/ma/belmont')
j = r.json()

print j['state']
print j['places'][1]['post code']

Places is a list and not a dictionary. This line below should therefore not work:

print(data['places']['latitude'])

You need to select one of the items in places and then you can list the place's properties. So to get the first post code you'd do:

print(data['places'][0]['post code'])

Tags:

Python

Json