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

felix001 picture felix001 · Jun 1, 2013 · Viewed 492.3k times · Source

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?

Answer

pswaminathan picture pswaminathan · Jun 1, 2013

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.