0

I am getting a data in response when I hit the API URL in django. I want to get content_id from that data to check whether it already exists or not. How to iterate the JSON to find the certain key and and its value?

r.json() =   

  {
        "code": 200,
        "status": "OK",
        "data": [
            {
                "cart_id": "36",
                "content": [
                    {
                        "price": "100",
                        "price_id": "1",
                        "code": "USD",
                        "symbol": "$",
                        "name": "Carol of the Bells SATB - arr. Jay Rouse",
                        "content_id": "17408"
                    }
                ],
                "poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
            }
        ],
        "msg": "Contents Found!"
    }

i tried r.json()['data'][0]['content'][0] but it works in only one data in, if there are more than one data then when i try to iterate content_id with for loop it doesnt work

5
  • What have you tried so far? Commented Sep 26, 2018 at 3:58
  • @KlausD. i have updated question. Commented Sep 26, 2018 at 4:03
  • You are going to show us the code that doesn't work? Commented Sep 26, 2018 at 4:06
  • @KlausD. is this a valid json file with the r.json() at the top, I'm familiar with navigating json but never seen it with that top line Commented Sep 26, 2018 at 4:08
  • 1
    @vash_the_stampede The assignment at the top is rubbish. I guess he wants to express that this the result if the given call. Commented Sep 26, 2018 at 4:16

3 Answers 3

2

General access

with open('vash.json') as f:
    data = json.load(f)

data['data'][0]['content'][0]['content_id'])

To iterate over

for i in data['data']:
    for j in i['content']:
        print(j['content_id'])

Bonus

print([j['content_id'] for i in data['data'] for j in i['content']])
17408
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe something like this could help. In the code you posted, the first line is a little bit different from what I have seen previously. Maybe json.loads will help you in getting the "content_id" for n number of objects in content.

import json

data = json.loads('''{
        "code": 200,
        "status": "OK",
        "data": [
            {
                "cart_id": "36",
                "content": [
                    {
                        "price": "100",
                        "price_id": "1",
                        "code": "USD",
                        "symbol": "$",
                        "name": "Carol of the Bells SATB - arr. Jay Rouse",
                        "content_id": "17408"
                    },
                    {
                        "price": "100",
                        "price_id": "1",
                        "code": "USD",
                        "symbol": "$",
                        "name": "Carol of the Bells SATB - arr. Jay Rouse",
                        "content_id": "999834"
                    }
                ],
                "poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
            },
            {
                "cart_id": "36",
                "content": [
                    {
                        "price": "100",
                        "price_id": "1",
                        "code": "USD",
                        "symbol": "$",
                        "name": "Carol of the Bells SATB - arr. Jay Rouse",
                        "content_id": "34523"
                    },
                    {
                        "price": "100",
                        "price_id": "1",
                        "code": "USD",
                        "symbol": "$",
                        "name": "Carol of the Bells SATB - arr. Jay Rouse",
                        "content_id": "6423412"
                    }
                ],
                "poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
            }
        ],
        "msg": "Contents Found!"
    }''')

for current_data in data["data"]:
    for current_content in current_data["content"]:
        # here I am printing, I imagine you will do something different
        # with the content_id.
        print(current_content["content_id"])

Comments

0

With python it's pretty easy to convert json to a dictionary and iterate it that way.

https://docs.python.org/2/library/json.html

Comments

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.