In this short guide, you'll see how to parse JSON responses from the Python requests library.
Here you can find the short answer:
(1) Using .json() method
import requests
response = requests.get(url)
data = response.json()
(2) With error handling
try:
data = response.json()
except requests.exceptions.JSONDecodeError:
print("Invalid JSON response")
(3) Convert to DataFrame
import pandas as pd
df = pd.DataFrame(response.json())
So let's see several useful examples on how to parse and work with JSON responses from APIs.
Suppose you receive a JSON response like:
{
"company": "Apple Inc",
"ticker": "AAPL",
"price": 185.64,
"change": 2.34
}
1: Parse JSON using .json() method
Let's start with the most straightforward method - using the built-in .json() method to parse API responses:
import requests
url = 'https://api.github.com/repos/python/cpython'
response = requests.get(url)
data = response.json()
print(f"Repository: {data['name']}")
print(f"Stars: {data['stargazers_count']}")
print(f"Language: {data['language']}")
print(f"Description: {data['description']}")
result will be:
Repository: cpython
Stars: 58429
Language: Python
Description: The Python programming language
The .json() method automatically deserializes the JSON response into a Python dictionary, making it easy to access values using standard dictionary syntax.
What if the response is not valid JSON? You'll get a JSONDecodeError. Always check the response first:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data['name'])
else:
print(f"Error: {response.status_code}")
result:
Repository: cpython
2: Parse JSON with error handling and validation
For production code, implement proper error handling to manage invalid JSON, network errors, and API failures:
import requests
from requests.exceptions import JSONDecodeError, RequestException
url = 'https://api.coingecko.com/api/v3/coins/bitcoin'
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
data = response.json()
print(f"Coin: {data['name']}")
print(f"Symbol: {data['symbol'].upper()}")
print(f"Current Price (USD): ${data['market_data']['current_price']['usd']:,.2f}")
print(f"Market Cap: ${data['market_data']['market_cap']['usd']:,.0f}")
except RequestException as e:
print(f"Network error: {e}")
except JSONDecodeError:
print("Response is not valid JSON")
except KeyError as e:
print(f"Missing expected field: {e}")
result:
Coin: Bitcoin
Symbol: BTC
Current Price (USD): $43,567.23
Market Cap: $851,234,567,890
This approach handles:
- Network failures with
RequestException - Invalid JSON with
JSONDecodeError - Missing keys with
KeyError - HTTP errors with
raise_for_status()
3: Parse nested JSON and convert to DataFrame
When working with complex nested JSON from APIs, convert the response to a pandas DataFrame for easier analysis:
import requests
import pandas as pd
url = 'https://api.github.com/users/torvalds/repos'
response = requests.get(url)
repos = response.json()
df = pd.DataFrame(repos)
df_subset = df[['name', 'language', 'stargazers_count', 'forks_count']]
df_sorted = df_subset.sort_values('stargazers_count', ascending=False).head(5)
print("Top 5 repositories:")
print(df_sorted.to_string(index=False))
result:
| name | language | stargazers_count | forks_count |
|---|---|---|---|
| linux | C | 158432 | 48234 |
| subsurface | C | 2341 | 567 |
| libdc-for-dirk | C | 234 | 89 |
| test-tlb | C | 156 | 45 |
| uemacs | C | 134 | 23 |
This converts the JSON array into a structured DataFrame, enabling easy sorting, filtering, and analysis with pandas methods.
For deeply nested JSON, use json_normalize:
from pandas import json_normalize
response = requests.get('https://api.example.com/complex-data')
data = response.json()
df = json_normalize(data, record_path=['items'], meta=['timestamp', 'status'])
print(df.head())
This flattens nested structures into a tabular format automatically.
4: Extract specific fields from JSON response
When you only need specific fields from a large JSON response, extract them directly:
import requests
url = 'https://api.github.com/users/google'
response = requests.get(url)
if response.ok:
user = response.json()
profile = {
'username': user.get('login'),
'name': user.get('name'),
'company': user.get('company'),
'repos': user.get('public_repos'),
'followers': user.get('followers')
}
for key, value in profile.items():
print(f"{key.capitalize()}: {value}")
result:
Username: google
Name: Google
Company: @google
Repos: 2543
Followers: 145678
Using .get() with default values prevents KeyError exceptions when fields are missing from the API response.
5: Handle JSON arrays and list comprehension
When the API returns a JSON array, use list comprehension to process multiple items:
import requests
url = 'https://jsonplaceholder.typicode.com/posts'
response = requests.get(url)
posts = response.json()
titles = [post['title'] for post in posts[:5]]
user_ids = {post['userId'] for post in posts}
print("First 5 post titles:")
for i, title in enumerate(titles, 1):
print(f"{i}. {title[:50]}...")
print(f"\nUnique users: {len(user_ids)}")
result:
First 5 post titles:
1. sunt aut facere repellat provident occaecati excep...
2. qui est esse...
3. ea molestias quasi exercitationem repellat qui ipu...
4. eum et est occaecati...
5. nesciunt quas odio...
Unique users: 10
This efficiently extracts data from JSON arrays without creating intermediate data structures.