Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a JSON array in Python

Tags:

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.

like image 236
Abdul Avatar asked Feb 24 '17 17:02

Abdul


People also ask

Can you loop through JSON in Python?

Use json. loads() With the Help of the for Loop to Iterate Through a JSON Object in Python. A built-in package, json , is provided by Python, which can be imported to work with JSON form data. In Python, JSON exists as a string or stored in a JSON object.

How do you traverse through a JSON array in Python?

Looping through a JSON array We use For Loop function to iterate through a JSON array after creating it. We first, import the json library by using the import function and then create a function using def keyword. After initializing the function we create a JSON array by using a single quote with curly brackets{}.

Is looping an array is possible in JSON?

Looping Using JSON JSON stands for JavaScript Object Notation. It's a light format for storing and transferring data from one place to another. So in looping, it is one of the most commonly used techniques for transporting data that is the array format or in attribute values.

How do you iterate through an array of objects in Python?

You can use the for in loop to loop through all the elements of an array.


1 Answers

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

for restaurant in data['restaurants']:     print restaurant['restaurant']['name'] 
like image 132
Daniel Avatar answered Sep 22 '22 06:09

Daniel