0

this particular question is asked quiet frequently and i have truly tried all the different methods without any luck.

Here is my json response.And Im trying to retrieve the value of cuisine_id and cuisine_name field.

{
  "cuisines": [
    {
      "cuisine": {
        "cuisine_id": 1,
        "cuisine_name": "American"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 2,
        "cuisine_name": "Andhra"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 4,
        "cuisine_name": "Arabian"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 3,
        "cuisine_name": "Asian"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 5,
        "cuisine_name": "Bakery"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 270,
        "cuisine_name": "Beverages"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 7,
        "cuisine_name": "Biryani"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 168,
        "cuisine_name": "Burger"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 30,
        "cuisine_name": "Cafe"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 25,
        "cuisine_name": "Chinese"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 35,
        "cuisine_name": "Continental"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 100,
        "cuisine_name": "Desserts"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 38,
        "cuisine_name": "European"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 40,
        "cuisine_name": "Fast Food"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 271,
        "cuisine_name": "Finger Food"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 233,
        "cuisine_name": "Ice Cream"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 55,
        "cuisine_name": "Italian"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 164,
        "cuisine_name": "Juices"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 62,
        "cuisine_name": "Kerala"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 66,
        "cuisine_name": "Lebanese"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 72,
        "cuisine_name": "Mangalorean"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 75,
        "cuisine_name": "Mughlai"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 50,
        "cuisine_name": "North Indian"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 82,
        "cuisine_name": "Pizza"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 1005,
        "cuisine_name": "Roast Chicken"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 83,
        "cuisine_name": "Seafood"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 85,
        "cuisine_name": "South Indian"
      }
    },
    {
      "cuisine": {
        "cuisine_id": 90,
        "cuisine_name": "Street Food"
      }
    }
  ]
}

I was able to get the cuisine data with following code

import requests
from pprint import pprint
import json




apiUrl = "https://<blahblah>/api/v2.1/cuisines?city_id=31"
header = {"User-agent":"curl/7.43.0", "Accept":"application/json","user-key":"API_KEY"}

response = requests.get(apiUrl, headers=header)

binary = response.content
data = json.loads(binary)


for item in data['cuisines']:
    #print "item is" + str(item)
    for k,v in item['cuisine'].items():
       print(k,v)

output

(u'cuisine_name', u'American')
(u'cuisine_id', 1)
(u'cuisine_name', u'Andhra')
(u'cuisine_id', 2)
(u'cuisine_name', u'Arabian')
(u'cuisine_id', 4)
(u'cuisine_name', u'Asian')
(u'cuisine_id', 3)
(u'cuisine_name', u'Bakery')
(u'cuisine_id', 5)
(u'cuisine_name', u'Beverages')
(u'cuisine_id', 270)
(u'cuisine_name', u'Biryani')
(u'cuisine_id', 7)
(u'cuisine_name', u'Burger')
(u'cuisine_id', 168)
(u'cuisine_name', u'Cafe')
(u'cuisine_id', 30)
(u'cuisine_name', u'Chinese')
(u'cuisine_id', 25)
(u'cuisine_name', u'Continental')
(u'cuisine_id', 35)
(u'cuisine_name', u'Desserts')
(u'cuisine_id', 100)
(u'cuisine_name', u'European')
(u'cuisine_id', 38)
(u'cuisine_name', u'Fast Food')
(u'cuisine_id', 40)
(u'cuisine_name', u'Finger Food')
(u'cuisine_id', 271)
(u'cuisine_name', u'Ice Cream')
(u'cuisine_id', 233)
(u'cuisine_name', u'Italian')
(u'cuisine_id', 55)
(u'cuisine_name', u'Juices')
(u'cuisine_id', 164)
(u'cuisine_name', u'Kerala')
(u'cuisine_id', 62)
(u'cuisine_name', u'Lebanese')
(u'cuisine_id', 66)
(u'cuisine_name', u'Mangalorean')
(u'cuisine_id', 72)
(u'cuisine_name', u'Mughlai')
(u'cuisine_id', 75)
(u'cuisine_name', u'North Indian')
(u'cuisine_id', 50)
(u'cuisine_name', u'Pizza')
(u'cuisine_id', 82)
(u'cuisine_name', u'Roast Chicken')
(u'cuisine_id', 1005)
(u'cuisine_name', u'Seafood')
(u'cuisine_id', 83)
(u'cuisine_name', u'South Indian')
(u'cuisine_id', 85)
(u'cuisine_name', u'Street Food')
(u'cuisine_id', 90)

But, what I wanted was separate values like 90, streetfood. How would I achieve it?

Is there any other way?

Following change gives me

TypeError: string indices must be integers

for item in data['cuisines']:
    #print "item is" + str(item)
    for ditem in item['cuisine']:
        print ditem['cuisine_name']
        print ditem['cuisine_id']
1
  • Try replacing the inner for loop with print(list(item['cuisine'].values())) Commented Apr 24, 2017 at 12:46

3 Answers 3

2

try:

for item in data['cuisines']:
   for k,v in item.items():
     print('{}, {}'.format(v['cuisine_id'], v['cuisine_name']))
Sign up to request clarification or add additional context in comments.

2 Comments

your are missing closing parenthesis
Thank you @jyotish
0
for item in data['cuisines']:
    print(*item['cuisine'].values(),sep=', ')

Comments

0

I would like to answer for the below error

TypeError: string indices must be integers

This should fix the error

for item in data['cuisines']:
    for ditem in item:
        print item[ditem]['cuisine_name']
        print item[ditem]['cuisine_id']

1 Comment

Thank you @amarnath

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.