Data Structures18 min read
Data in JSON Style
Work with JSON-like dict/list structures: safe reading, transformations, and building clean nested data for APIs and real applications.
David Miller
October 18, 2025
7.1k276
Most APIs return JSON, which becomes nested dict/list in Python.
Example JSON-like structure
data = {
"users": [
{"id": 1, "profile": {"name": "Tom"}},
{"id": 2, "profile": {"name": "Sarah"}},
]
}
Read nested data safely
users = data.get("users", [])
for u in users:
name = u.get("profile", {}).get("name", "Unknown")
print(name)
Transform: build id -> name map
id_to_name = {}
for u in data.get("users", []):
uid = u.get("id")
name = u.get("profile", {}).get("name")
if uid is not None and name is not None:
id_to_name[uid] = name
print(id_to_name)
Graph: JSON mapping
flowchart LR
A[users list] --> B[loop]
B --> C[id]
B --> D[name]
C --> E[dict map]
D --> E
Remember
- JSON becomes dict + list
- Use get() to avoid KeyError
- Building maps is a common real-world pattern
#Python#Intermediate#Nested#JSON