4. JSON

JSON is a text format for exchanging data. Python values and JSON text are converted with the json module:

  • obj — the Python value to convert: usually a dict, list, string, number, boolean or None.
  • s — a string that contains JSON.
  • fp — an open file object in text mode. The name stands for file pointer.
  • indent — optional and keyword-only. Default: None, which produces single-line JSON. An integer such as 2 pretty-prints with that many spaces per level; pass it by name, indent=2.

The s in dumps and loads means string. Without the s, dump and load work with a file object instead of a string.

JSON and Python use similar types:

  • JSON object → Python dict
  • JSON array → Python list
  • JSON string → Python str
  • JSON number → Python int or float
  • JSON true / false → Python True / False
  • JSON null → Python None

JSON keys and strings always use double quotes, even though Python also accepts single quotes. json.dumps() produces valid JSON for you.

Coming from JavaScript? json.dumps is JSON.stringify, and json.loads is JSON.parse.

#01

Turn a dict into JSON

json.dumps() returns a JSON string for a Python value.

Import json. Create the dictionary {"name": "Ada", "age": 36} and print the result of json.dumps() on it.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Turn JSON into a dict

json.loads() takes a JSON string and returns the matching Python value. A JSON object becomes a dict.

Import json. Parse the string '{"city": "Paris", "population": 2148000}' with json.loads(). Store the result in a variable and print the value of its city key.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Read a JSON array

A JSON array becomes a Python list. You can index it like any other list.

Import json. Parse the string '["python", "json", "file"]' with json.loads() and print its second element using an index.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Pretty print with indent

json.dumps() with indent=N uses N spaces of indentation and puts each part on its own line. Pass indent by name.

Import json. Print the dictionary {"name": "Ada", "age": 36} converted with json.dumps() and indent=2.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#05

Write JSON to a file

json.dump() writes JSON to an open file object. Unlike dumps(), it does not return a string. The s is the difference: dumps/loads work with strings, dump/load work with files.

Import json. Use a with statement to open /tmp/person.json in "w" mode. Inside the block, call json.dump() with the dictionary {"name": "Ada", "age": 36} and the file object. Then print Saved.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#06

Read JSON from a file

json.load() reads JSON from an open file object and returns the matching Python value.

First write the dictionary {"name": "Ada", "age": 36} to /tmp/person.json with json.dump() as in the previous exercise. Then use a second with statement to open the same file in "r" mode and call json.load(). Print the value of the age key.

Print exactly:

Create a free account to save your progress and unlock chapter 2.