What's the best way to parse a JSON response from the requests library?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle
--
Chapters
00:00 Question
00:34 Accepted answer (Score 428)
01:02 Answer 2 (Score 690)
01:22 Answer 3 (Score 25)
02:06 Thank you
--
Full question
https://stackoverflow.com/questions/1687...
Question links:
[requests]: https://requests.readthedocs.io/
Accepted answer links:
[json.loads]: http://docs.python.org/2/library/json.ht...
Answer 2 links:
[json]: https://requests.readthedocs.io/en/lates...
[autodetects which decoder to use]: https://github.com/requests/requests/blo...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json #rest #pythonrequests
#avk47
ANSWER 1
Score 735
Since you're using requests, you should use the response's json method.
import requests
response = requests.get(...)
data = response.json()
ACCEPTED ANSWER
Score 448
You can use json.loads:
import json
import requests
response = requests.get(...)
json_data = json.loads(response.text)
This converts a given string into a dictionary which allows you to access your JSON data easily within your code.
Or you can use @Martijn's helpful suggestion, and the higher voted answer, response.json().
ANSWER 3
Score 37
You can use the json response as dictionary directly:
import requests
res = requests.get('https://reqres.in/api/users?page=2')
print(f'Total users: {res.json().get("total")}')
or you can hold the json content as dictionary:
json_res = res.json()
and from this json_res dictionary variable, you can extract any value of your choice
json_res.get('total')
json_res["total"]
Attentions Because this is a dictionary, you should keep your eye on the key spelling and the case, i.e. 'total' is not the same as 'Total'
ANSWER 4
Score 1
What's the best way to parse a JSON response from the requests library?
The top answers show seemingly two different ways to parse a json response into a Python object but they are essentially the same.
response.json() differs in two places:
- it uses
simplejson(which is the externally maintained development version of the json library included with Python) if it's installed in your environment, but uses the built-injsonif not (source). I think there used to be a performance difference betweenjsonandsimplejsonin the past (when Python 2 was still widely used) but there's almost no difference between the libraries anymore. - if the response doesn't have an encoding (
response.encodingis None), then it tries to guess it and try to decode using the guessed encoding (source).
So in 99.9999% of cases, response.json() and json.loads(response.text) will produce the same dictionary. It may differ if the source json has some weird encoding.