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
dumpsmeans dump to string;loadsmeans load from string- Python
TrueandNonebecome JSONtrueandnull - 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.

