Looping through a JSON array in Python

with open('data.json') as data_file:    
data = json.load(data_file)
for restaurant in data['restaurant']:
    print restaurant['restaurant']['name']

This way you will loop over the elements in the list of dictionaries inside your 'restaurants' field and output their names.

You were really close, what you were doing before was looping over all the main fields in your json file and print the name of the first restaurant every time (data['restaurants'][0] gives you the first restaurant in the list of restaurants... and you printed its name every time)


When restaurants is your list, you have to iterate over this key:

for restaurant in data['restaurants']:
    print restaurant['restaurant']['name']

Tags:

Python

Json