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


Example

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

response = requests.get('http://example.com/')
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()
else:
    non_json_result = response.data