PYTHON
Parse Nested JSON API Responses in Python
Discover how to efficiently parse and extract specific data from deeply nested JSON structures returned by external APIs using Python's requests library and dictionary access.
import requests
import json
def fetch_and_parse_nested_json(api_url):
"""
Fetches data from a given API URL and parses nested JSON responses.
"""
try:
response = requests.get(api_url)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
data = response.json()
return data
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return None
# Example API URL (returns complex, nested JSON data)
# Using a mock API for demonstration: https://jsonplaceholder.typicode.com/users
# Or a more deeply nested structure: https://api.publicapis.org/entries (large, might be slow)
# For a simpler nested example, let's construct one.
example_api_url = "https://mocki.io/v1/d4867d8b-b5d5-4a48-a4ab-79131b5809b8" # A custom mock with nested data
# Expected structure from mocki.io/v1/d4867d8b-b5d5-4a48-a4ab-79131b5809b8:
# {
# "status": "success",
# "metadata": {
# "timestamp": "2023-10-27T10:00:00Z",
# "source": "example_api"
# },
# "data": {
# "items": [
# {
# "id": "item-1",
# "name": "Widget A",
# "details": {
# "price": 29.99,
# "currency": "USD",
# "availability": {
# "warehouse_a": 100,
# "warehouse_b": 50
# }
# },
# "tags": ["electronics", "gadget"]
# },
# {
# "id": "item-2",
# "name": "Gadget X",
# "details": {
# "price": 12.50,
# "currency": "EUR",
# "availability": {
# "warehouse_a": 200,
# "warehouse_b": 0
# }
# }
# "tags": ["tools"]
# }
# ],
# "total_count": 2
# }
# }
# Fetch data
api_data = fetch_and_parse_nested_json(example_api_url)
if api_data:
print("Successfully fetched and parsed data.")
# Accessing top-level keys
status = api_data.get('status')
print(f"Status: {status}")
# Accessing nested metadata
timestamp = api_data.get('metadata', {}).get('timestamp')
print(f"Timestamp: {timestamp}")
# Accessing deeply nested data
items = api_data.get('data', {}).get('items', [])
if items:
print("
Processing items:")
for item in items:
item_id = item.get('id')
item_name = item.get('name')
price = item.get('details', {}).get('price')
currency = item.get('details', {}).get('currency')
warehouse_a_stock = item.get('details', {}).get('availability', {}).get('warehouse_a')
tags = item.get('tags', [])
print(f" Item ID: {item_id}")
print(f" Name: {item_name}")
print(f" Price: {price} {currency}")
print(f" Warehouse A Stock: {warehouse_a_stock}")
print(f" Tags: {', '.join(tags)}")
print("-" * 20)
else:
print("No items found in data.")
else:
print("Failed to retrieve API data.")
How it works: This Python snippet demonstrates how to fetch data from a REST API and parse its nested JSON response. It uses the `requests` library to make an HTTP GET request and automatically parses the JSON response into Python dictionaries and lists. The example then illustrates safe access to deeply nested elements using the `.get()` method with a default empty dictionary or list, which prevents `KeyError` if an intermediate key is missing. This approach is crucial for robust API integrations where response structures can sometimes vary.