Parsing JSON Responses from APIs in Python Requests

Feb 3, 2024 ยท 2 min read

When working with APIs in Python, you'll often get JSON data back in the response. The requests library makes it easy to parse this JSON and access the data you need.

Use response.json()

The simplest way is to call the .json() method on the response object. For example:

import requests

response = requests.get('https://api.example.com/data')
data = response.json()
print(data['key1']) 

This automatically parses the JSON and returns a Python dict that you can access just like any other dict.

Handling invalid JSON

If the API returns invalid JSON, .json() will raise a JSONDecodeError exception. You can handle this gracefully:

try:
  data = response.json()
except JSONDecodeError:
  print('Response was not valid JSON')

Accessing response status codes

It's good practice to check the status code before trying to parse the response. For example:

if response.status_code == 200:
  data = response.json()
else:
  print(f'Error with status code {response.status_code}')

This makes sure you only try to parse valid responses.

Response Content Type

Also check that the Content-Type header is application/json before parsing to avoid issues.

In Summary

  • Use response.json() to easily parse JSON from API responses
  • Wrap in try/catch to handle invalid JSON gracefully
  • Check status codes before parsing responses
  • Verify Content-Type is JSON before parsing
  • Parsing JSON from APIs is smooth with Python requests! Proper error handling avoids surprises.

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!