0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

pls tell how would be if we need to get multiple fields?

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.