Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

JSON: The Language of APIs

14 min 30 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 2 of 5 · View course roadmap
Step 1

Learn the idea

JSON (JavaScript Object Notation) is how APIs exchange data — and it maps almost 1:1 to Python:

  • JSON object {} ↔ Python dict
  • JSON array [] ↔ Python list
  • nullNone, true/falseTrue/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") beats data["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.

Step 2

Try it yourself

Blank · autosaved

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”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +30 XP

Blank · autosaved

Parse the JSON string in the starter and print: the username, the number of repos, and the name of the first repo.

Target output
ada-lovelace
2
analytics-engine
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. JSON null becomes what in Python?
2. Which converts a Python dict into a JSON string?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path