Search code examples
pythonjsonrestpython-requests

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


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?


Solution

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