JSON: The Language of APIs
Lesson 2 of 5 · View course roadmap
Learn the idea
JSON (JavaScript Object Notation) is how APIs exchange data — and it maps almost 1:1 to Python:
- JSON object
{}↔ Pythondict - JSON array
[]↔ Pythonlist null↔None,true/false↔True/False
Two functions do all the work: json.loads(text) parses a JSON string into Python objects; json.dumps(obj) serialises Python back to a JSON string (indent=2 pretty-prints).
Real API responses nest deeply — practise the drill-down: data["results"][0]["name"]. Sketch the shape first, then write the path.
Where you'll use this
JSON is the wire format of the modern web — REST APIs, webhooks, config files (VS Code settings!), NoSQL documents. Data engineers spend whole careers reshaping exactly this.
Common mistakes
- JSON syntax is stricter than Python: double quotes only, no trailing commas, no comments — valid Python dict literals can be invalid JSON.
- json.loads(f) on a file object — that's json.load(f); loads takes a string.
- Assuming a field exists: real APIs omit optional fields —
data.get("email")beatsdata["email"]for anything not guaranteed.
Pro tip
Debug any nested payload instantly with print(json.dumps(data, indent=2)) — structure jumps out. From a terminal, piping curl output through python -m json.tool does the same.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +30 XP
Parse the JSON string in the starter and print: the username, the number of repos, and the name of the first repo.
ada-lovelace 2 analytics-engine
data["repos"] is a list of dicts — index it with [0] then ["name"].
import json
raw = '{"user": "ada-lovelace", "repos": [{"name": "analytics-engine", "stars": 42}, {"name": "sqltrainer", "stars": 17}]}'
data = json.loads(raw)
print(data["user"])
print(len(data["repos"]))
print(data["repos"][0]["name"])
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.