How to extract an specific field from a JSON file with Python

I am trying to download some JSON files from an URL and then extract an specific field from this files.

This is what I have done so far:

import requests
r = requests.get('https://api.someURL')
if r.status_code != 200:
    print("Failure")
    break
else:

And here is where I am stucked. This code is supposed to retreive a JSON-formatted list with a lot of information. Something like this:

[
{          
   "id": "a12345",
   "size": "58457888",
   "status":"AVAILABLE",
   "uri": "https://api.output:"output file"
},
{
...
]

I dont know how long the list will be, but what I am trying is to get, for every element in the list, the information stored in the field "uri".

¿Any idea of how can I get this?

Sorry if I am asking for too much help, but I am not used to work with JSON files.

Thank you very much in advance, Álvaro

Upvotes: 0

Views: 3536

Answers (1)

agabel
agabel

Reputation: 102

The response object contains a json() method to decode the JSON response into a usable Python object. Based on your example data it should be a list of dictionaries.

import requests
r = requests.get('https://api.someURL')
if r.status_code != 200:
    print("Failure")
    break
else:
    data = r.json()
    for record in data:
        uri = record.get('uri')
        print(uri) # or do whatever you need to do with it

Upvotes: 2

Related Questions