0

I want to fetch the value of each api3 in this json object where each array has api3 value.

 {
    "count": 10,
    "result": [
    {
            "type": "year",
            "year": {
                "month": {
                    "api1": {
                        "href": "https://Ap1.com"
                    },
                    "api2": {
                        "href": "FETCH-CONTENT"
                    },
                    "api3": {
                        "href": "https://Ap3.com"
                    },
                    "api4": {
                        "href": "https://Ap4.com"
                    }
                },
                "id": "sdvnkjsnvj",
                "summary": "summeryc",
                "type": "REST",
                "apiId": "mlksmfmksdfs",
                "idProvider": {
                    "id": "sfsmkfmskf",
                    "name": "Apikey"
                },
                "tags": []
            }
        },
        {
            "type": "year1",
            "year": {
                "month": {
                    "api1": {
                        "href": "https://Ap11.com"
                    },
                    "api2": {
                        "href": "FETCH-CONTENT-1"
                    },
                    "api3": {
                        "href": "https://Ap13.com"
                    },
                    "api4": {
                        "href": "https://Ap14.com"
                    }
                },
                "id": "sdvnkjsnvj",
                "summary": "summeryc",
                "type": "REST",
                "apiId": "mlksmfmksdfs",
                "idProvider": {
                    "id": "sfsmkfmskf",
                    "name": "Apikey"
                },
                "tags": []
            }
        },

I am able to get the whole json object and first value inside it.

with open('C:\python\examplee.json','r+') as fr:
data = json.load(fr)
print(data["result"])

Thank you in advance for helping me figuring this.

2
  • print([i["year"]["month"]["api3"]["href"] for i in data["result"]])? Commented May 13, 2019 at 13:42
  • You just need to choose the correct indexes and keys to deal with @RJ_Singh check my answer below :) Commented May 13, 2019 at 14:03

2 Answers 2

1

For each element in list of result key, get the value for the nested dictionary within item

print([item['year']['month']['api3'] for item in data['result']])

The output will be [{'href': 'https://Ap3.com'}, {'href': 'https://Ap13.com'}]

Or if you want to get the href value as well

print([item['year']['month']['api3']['href'] for item in data['result']])

The output will be

['https://Ap3.com', 'https://Ap13.com']

So your whole code will look like

data = {}
with open('C:\python\examplee.json','r+') as fr:
    data = json.load(fr)

print([item['year']['month']['api3']['href'] for item in dct['result']])
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Devesh.. But it is not working i tried putting multiple combination to get the href option but no luck so far.
Your solution works absolutely fine @Devesh with the json provided in the question.Now i am facing difficulty because there is slight changes in my json file. All fields are same in all the elements except one of the fields doesn't match.That is why it is not working.
Absolutely @Devesh Thanks a bunch ..:)
But what would be the approach if we have json file with the same file content and one element is having different field.
|
1

Looks like your JSON schema is static so you can just use this:

print([x['year']['month']['api3']['href'] for x in data['result']])

will return you:

['https://Ap3.com', 'https://Ap13.com']

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.