python-requests Sending and receiving JSON Receiving JSON in a response


When a response contains valid JSON, just use the .json() method on the Response object to get the decoded result:

response = requests.get('')
decoded_result = response.json()

However, this does not fail gracefully; it will raise a JSONDecodeError if the response object is not JSON-parseable.

You may wish to first check the content MIME type, for more graceful error handling:

if 'application/json' in response.headers['Content-Type']:
    decoded_result = response.json()
    non_json_result =