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:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: