393

I'm using the python requests module to send a RESTful GET to a server, for which I get a response in JSON. The JSON response is basically just a list of lists.

What's the best way to coerce the response to a native Python object so I can either iterate or print it out using pprint?

0

3 Answers 3

696

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.

12
  • 2
    Just keep in mind that it have appeared somewhere in between v0.12 and v1.0 so that for example Ubuntu 12.04 deb-package for python-requests does not have this function yet (it is v0.8). You can pip install requests though instead of using deb package.
    – timurb
    Oct 22, 2013 at 16:11
  • 8
    I am a little curious what is now data is it a list of list or a dictionary. May 30, 2015 at 18:05
  • 11
    @Krishna_Oza data mirrors the structure of the JSON it's reading. For example, if the response is: [{"a": {...}}], data is a list, with list[0] == {'a': {...}}. If the response is {"a": "b", "c": "d"}, data is a dict. Does that answer your question? May 30, 2015 at 21:06
  • 1
    @CGFoX what version are you running? I'm still seeing the API work the same way on the latest version: >>> import requests >>> r = requests.get('http://httpbin.org/get') >>> r.json <bound method Response.json of <Response [200]>> >>> r.json() {'args': {}, ...} Oct 29, 2018 at 14:47
  • 6
    Upvote for this because I was usually using the json.loads(response.text) method until on some large jsons I found that using respons.json() was much faster than the other way.
    – Paul R.
    Jan 28, 2019 at 14:01
428

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

8
  • 3
    ok great, however each of the elements would still be unicode.
    – felix001
    Jun 1, 2013 at 21:43
  • @felix001: yes, although you can convert any data using str(). On the other hand unicode data isn't bad to have around (in preparation for the future). Jun 1, 2013 at 21:45
  • 221
    Much, much better to use response.json(), as it'll do a better job of figuring out the encoding used. (Disclaimer, I wrote some of that code).
    – Martijn Pieters
    May 19, 2016 at 20:24
  • @MartijnPieters: then how can I use requests json parser later on a memcached text of the response? i.e. having the output of response.text()?
    – neurino
    Mar 28, 2019 at 14:40
  • 1
    @neurino you want the standard library json module. The requests.compat module is there to bridge different Python versions and requests.compat.json is the same thing as json on practically every system you’d care about.
    – Martijn Pieters
    Mar 28, 2019 at 15:13
28

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'

1
  • json().get() ist the part I was looking for - for half an hour. Thank you!
    – ESP32
    Feb 16 at 19:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.