Parsing API Responses (JSON)
API responses are typically JSON. Learn to parse, navigate, and extract data from complex JSON structures.
10 min•By Priygop Team•Updated 2026
Parsing JSON Responses
Parsing JSON Responses
import json
# Simulated API response
api_response = {
"status": "success",
"data": {
"users": [
{"id": 1, "name": "Alice", "posts": [
{"title": "Python Tips", "likes": 42},
{"title": "API Guide", "likes": 28}
]},
{"id": 2, "name": "Bob", "posts": [
{"title": "Testing 101", "likes": 35}
]},
],
"pagination": {"page": 1, "total": 50}
}
}
# Navigate the response
users = api_response["data"]["users"]
print(f"Total users on page: {len(users)}")
# Extract specific data
for user in users:
post_count = len(user["posts"])
total_likes = sum(p["likes"] for p in user["posts"])
print(f" {user['name']}: {post_count} posts, {total_likes} likes")
# Find most liked post
all_posts = [(u["name"], p["title"], p["likes"])
for u in users for p in u["posts"]]
top_post = max(all_posts, key=lambda x: x[2])
print(f"\nMost liked: '{top_post[1]}' by {top_post[0]} ({top_post[2]} likes)")
# Safe access with .get()
page = api_response.get("data", {}).get("pagination", {}).get("page", 1)
print(f"Page: {page}")Tip
Tip
Use query parameters for filtering: requests.get(url, params={'page': 2, 'limit': 10}). It's cleaner than building URLs manually.
Diagram
Loading diagram…
undefined/functions dropped. Always try/catch parse.
Common Mistake
Warning
Not URL-encoding query parameters. Use requests.get(url, params=dict) which handles encoding automatically.
Quick Quiz
Practice Task
Note
(1) Build a paginated API call. (2) Add search query parameters. (3) Handle empty results gracefully.