2

I am trying to access a JSON value from a python script. the value I am trying to access is "name" only, would really appreciate help in how can I do that using a python script the JSON data file is:

"restaurants": [
{
  "restaurant": {
    "R": {
      "res_id": 9101083
    },
    "id": "9101083",
    "name": "My Meat Wagon",
    "url": "https://www.zomato.com/dublin/my-meat-wagon-smithfield?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
    "location": {
      "address": "Market Square, Smithfield, Dublin Dublin 7",
      "locality": "Smithfield",
      "city": "Dublin",
      "city_id": 91,
      "latitude": "53.3489980000",
      "longitude": "-6.2788120000",
      "zipcode": "Dublin 7",
      "country_id": 97,
      "locality_verbose": "Smithfield, Dublin"
    },

in my test.py file I have

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

what should I do do have the program get the name of a restaurant only?

0

1 Answer 1

2

Simply writing data["restaurant"]["name"] doesn't do anything meaningful with the dictionary yet, it just looks it up, returns the value and then does nothing with it. In particular, it does not change what data refers to. If you wanted that, you would have to write:

data = data["restaurant"]["name"]

I don't recommend doing that though, because you lose the ability to access anything else inside the json structure. Instead, just print the value:

print data["restaurant"]["name"]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank, I got it to work to display one place, when I add a second JSON object to the file and try to run the program I am getting : raise ValueError(errmsg("Extra data", s, end, len(s))) my current JSON file is : ` { "restaurant": { "R": { "res_id": 9101083 }, "name": "My Meat Wagon" } }, { "restaurant": { "R": { "res_id": 9101628 }, "name": "Wowburger" } } `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.