import requests
import json
with requests.get("https://api.example.com/large", stream=True) as r:
for line in r.iter_lines():
if line:
record = json.loads(line)
print(record)
Option B: Use ijson (Incremental parser)
Best for HUGE files (100MB+)
import ijson
with open("big.json", "r") as f:
for obj in ijson.items(f, "item"):
print(obj)
Convert API Response to Python Dataclass (Clean & Efficient)
import requests
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
response = requests.get("https://api.example.com/user").json()
user = User(**response)
print(user.name)
Advantages:
Cleaner code
Type checking
IDE autocompletion
Use Pydantic for Faster + Validated Parsing (Production-Ready)
from pydantic import BaseModel
import requests
class User(BaseModel):
id: int
name: str
data = requests.get("https://api.example.com/user").json()
user = User(**data)
print(user.name)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Use
requests+.json()(Most Common & Efficient)Why it’s efficient?
response.json()uses Python’s optimized JSON decoder internally.Validate Success Before Parsing
Use
json.loads()When You Already Have a JSON StringHandle Large JSON Efficiently (Streaming)
For large datasets:
Option A: Stream using requests
Option B: Use
ijson(Incremental parser)Best for HUGE files (100MB+)
Convert API Response to Python Dataclass (Clean & Efficient)
Advantages:
Use Pydantic for Faster + Validated Parsing (Production-Ready)
Pydantic:
When Performance REALLY Matters (ujson)
ujsonis 2–4× faster than Python’s built-injson.