Skip to main content

Python JSON

JSON is a format for saving data and passing it between systems.

It is plain text that uses a simple object notation.


JSON in Python

Python ships with a built-in package named json for handling JSON data.

Example

Start by importing the json module:
main.py
import json

Parse JSON - Convert from JSON to Python

A JSON string can be parsed with the json.loads() method.

In this example, what comes back is a Python dictionary.

Example

Turn a JSON string into Python data:
Prompt
Parse a JSON string into a Python dictionary using json.loads(). The JSON string contains name, age, and city. Print the age value.
Output: complete Python program

Convert from Python to JSON

Going the other way, the json.dumps() method turns a Python object into a JSON string.

These Python types can be converted into JSON strings:

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None

During the conversion, each Python object becomes its JSON equivalent:

  • dict → Object
  • list → Array
  • tuple → Array
  • str → String
  • int → Number
  • float → Number
  • True → true
  • False → false
  • None → null

Example

Turn a Python dictionary into a JSON string:
Prompt
Convert a Python dictionary with name, age, and city into a JSON string using json.dumps(). Print the resulting JSON string.
Output: complete Python program

Example

Convert one Python object of each type to a JSON string, printing every result:
Prompt
Convert Python objects of different types (dict, list, tuple, string, int, float, True, False, None) into JSON strings. Use json.dumps() for each and print the results.
Output: complete Python program

Example

Convert a single Python object that uses every legal data type:
Prompt
Convert a Python object containing all legal data types (string, int, bool, tuple, None, list of dicts) into a JSON string. Print the result using json.dumps().
Output: complete Python program

Format the Result

The JSON string printed above is hard on the eyes, because it has no indentation or line breaks.

json.dumps() takes parameters that make the output more readable:

The separators can be customized too. Their default is (", ", ": "): a comma and a space between objects, and a colon and a space between keys and values:

Example

Set the indent parameter to choose how many spaces each level is indented:
Prompt
Use json.dumps() with the indent parameter set to 4 to format JSON output with indentation.
Output: complete Python program

Example

Replace the default separators through the separators parameter:
Prompt
Use json.dumps() with indent=4 and custom separators=(". ", " = ") to change the default separator.
Output: complete Python program

Order the Result

Other parameters of json.dumps() control the order of the keys in the output:

Example

Set the sort_keys parameter to choose whether the output gets sorted:
Prompt
Use json.dumps() with indent=4 and sort_keys=True to sort the keys in JSON output.
Output: complete Python program