Data in JSON Style
Work with JSON-like dict/list structures: safe reading, transformations, and building clean nested data for APIs and real applications.
Most APIs return JSON, which becomes nested dict/list in Python. ## Example JSON-like structure ```python data = { "users": [ {"id": 1, "profile": {"name": "Tom"}}, {"id": 2, "profile": {"name": "Sarah"}}, ] } ``` ## Read nested data safely ```python users = data.get("users", []) for u in users: name = u.get("profile", {}).get("name", "Unknown") print(name) ``` ## Transform: build id -> name map ```python 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 ```mermaid 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