39

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.

MaxPowers
  • 5,235
  • 2
  • 44
  • 69
Abdul
  • 1,537
  • 2
  • 13
  • 22

2 Answers2

80

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

for restaurant in data['restaurants']:
    print restaurant['restaurant']['name']
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • I am sending data using ajax to Django views as an array in the form of two values in a variable **legData** with values as `[1406, 1409]`. If I print to the console using `print(legData)` I get the output as **[1406,1409]**. However, if I try to parse the individual values of the list like `for idx, xLeg in enumerate(legData):` `print(idx, xLeg)`, I am getting an output of individual integers such as **[, 1, 4, 0, 6 ** and so on against the indices 0, 1, 2, 3, 4 etc. (yes, the square brackets & comma are also being output as if they were part of the data itself**). What is going wrong here? – Love Putin Not War May 15 '20 at 15:59
15
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)

Nassim Ben
  • 11,473
  • 1
  • 34
  • 52