The Python Oracle

What's the best way to parse a JSON response from the requests library?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 What'S The Best Way To Parse A Json Response From The Requests Library?
00:19 Accepted Answer Score 448
00:42 Answer 2 Score 735
00:56 Answer 3 Score 37
01:28 Answer 4 Score 1
02:27 Thank you

--

Full question
https://stackoverflow.com/questions/1687...

--

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()

It autodetects which decoder to use.




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-in json if not (source). I think there used to be a performance difference between json and simplejson in 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.encoding is 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.