Looping through a JSON array in Python
Question:
I have the following data taken from an API. I am trying to access the restaurant name using a Python script and have the script display it. Here are my files:
test.py
with open('data.json') as data_file:
data = json.load(data_file)
for restaurant in data:
print data ['restaurants'][0]['restaurant']['name']
my JSON file is as follows: (simplified)
{
"results_found": 3296,
"results_start": 0,
"results_shown": 20,
"restaurants": [
{
"restaurant": {
"R": {
"res_id": 9101083
},
"id": "9101083",
"name": "My Meat Wagon",
"address": "Market Square, Smithfield, Dublin Dublin 7",
"locality": "Smithfield",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3489980000",
"longitude": "-6.2788120000",
"zipcode": "Dublin 7",
"events_url": "https://www.zomato.com/dublin/my-meat-wagon-smithfield/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 9101628
},
"id": "9101628",
"name": "Wowburger",
"url": "https://www.zomato.com/dublin/wowburger-temple-bar?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "The Workmans Club, 11 Wellington Quay, Temple Bar, Dublin Dublin 2",
"locality": "The Workmans Club",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3452863158",
"longitude": "-6.2663815543",
"zipcode": "Dublin 2",
"country_id": 97,
"locality_verbose": "The Workmans Club, Dublin"
},
"switch_to_order_menu": 0,
"cuisines": "Burger",
"average_cost_for_two": 20,
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 16520426
},
"id": "16520426",
"name": "Brother Hubbard",
"locality_verbose": "North City, Dublin"
},
Currently, it displays the first restaurant name three times. I want it to loop through each restaurant
object and display the value for the key “name”. Any help would be appreciated.
Answers:
When restaurants
is your list, you have to iterate over this key:
for restaurant in data['restaurants']:
print restaurant['restaurant']['name']
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)
I have the following data taken from an API. I am trying to access the restaurant name using a Python script and have the script display it. Here are my files:
test.py
with open('data.json') as data_file:
data = json.load(data_file)
for restaurant in data:
print data ['restaurants'][0]['restaurant']['name']
my JSON file is as follows: (simplified)
{
"results_found": 3296,
"results_start": 0,
"results_shown": 20,
"restaurants": [
{
"restaurant": {
"R": {
"res_id": 9101083
},
"id": "9101083",
"name": "My Meat Wagon",
"address": "Market Square, Smithfield, Dublin Dublin 7",
"locality": "Smithfield",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3489980000",
"longitude": "-6.2788120000",
"zipcode": "Dublin 7",
"events_url": "https://www.zomato.com/dublin/my-meat-wagon-smithfield/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 9101628
},
"id": "9101628",
"name": "Wowburger",
"url": "https://www.zomato.com/dublin/wowburger-temple-bar?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "The Workmans Club, 11 Wellington Quay, Temple Bar, Dublin Dublin 2",
"locality": "The Workmans Club",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3452863158",
"longitude": "-6.2663815543",
"zipcode": "Dublin 2",
"country_id": 97,
"locality_verbose": "The Workmans Club, Dublin"
},
"switch_to_order_menu": 0,
"cuisines": "Burger",
"average_cost_for_two": 20,
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 16520426
},
"id": "16520426",
"name": "Brother Hubbard",
"locality_verbose": "North City, Dublin"
},
Currently, it displays the first restaurant name three times. I want it to loop through each restaurant
object and display the value for the key “name”. Any help would be appreciated.
When restaurants
is your list, you have to iterate over this key:
for restaurant in data['restaurants']:
print restaurant['restaurant']['name']
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)