Skip to content
dreamcode
dreamcode
Map
JSON
Lesson 42 of 77
+15 XP on finish
PYTHON INTERMEDIATEChapter 7 · Errors, Files and Modules

Working with JSON

JSON is the text format the web uses to send data around. The json module converts between JSON text and Python values: json.dumps(data) turns a dict or list into a JSON string, and json.loads(text) parses JSON text back into Python. Add indent=2 to make the text easy to read.

Worked example

How it reads

  • dumps means dump to string; loads means load from string
  • Python True and None become JSON true and null
  • Parsed JSON is ordinary Python: dicts, lists, strings and numbers
Common mistakes
  • JSON needs double quotes. json.loads("{'a': 1}") raises an error because of the single quotes.
Cloud tip: json.dump(data, file) and json.load(file) (no s) do the same thing straight to and from an open file.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Parse raw with json.loads and print just the hours: 42.

Press Run to check your work.