Working With JSON in Python: Read, Write, Parse, and Print JSON

JSON, short for JavaScript Object Notation, is an open standard. Although its name doesn’t imply so, it is a language-independent data format. JSON is used to both store and exchange data. It’s a prevalent data format because it is easy to read and write for humans too, although not as easy as YAML!

Working with JSON in Python is super easy! Python has two data types that, together, form the perfect tool for working with JSON in Python: dictionaries and lists.

Importing the JSON library in Python

Python ships with a powerful and elegant JSON library to help you decode and encode JSON. It can be imported with:

import json

This library is part of Python, so you don’t need to install it with the Pip package manager.

How to parse JSON in Python

Parsing a string of JSON data, also called decoding JSON, is as simple as using json.loads(…). Loads is short for load string.

It converts:

  • objects to dictionaries
  • arrays to lists,
  • booleans, integers, floats, and strings are recognized for what they are and will be converted into the correct types in Python
  • Any null will be converted into Python’s None type

Here’s an example of json.loads in action:

>>> import json
>>> jsonstring = '{"name": "erik", "age": 38, "married": true}'
>>> data = json.loads(jsonstring)
>>> print(data)
{'name': 'erik', 'age': 38, 'married': True}

The output might look like a string, but it’s actually a dictionary that you can use in your code as explained on our page about Python dictionaries. For example:

>>> type(data)
<class 'dict'>
>>> print('Hello', data['name'], "you're", data['age'], 'years old')
Hello erik you're 38 years old

Encoding JSON with Python

Encoding JSON data with Python is just as easy as decoding. Use json.dumps(…) (short for ‘dump to string’) to convert a Python object consisting of dictionaries, lists, and other native types into a string:

>>> data = {'name': 'erik', 'age': 38, 'married': True}
>>> json.dumps(data)
'{"name": "erik", "age": 38, "married": true}'

This is the same document, converted back to a string! If you want to make your JSON document more readable for humans, use the indent option. It will nicely format the JSON, using space characters:

>>> data = {'name': 'erik', 'age': 38, 'married': True}
>>> print(json.dumps(data, indent=2))
  "name": "erik",
  "age": 38,
  "married": true

Pretty printing JSON with the JSON module

Python’s JSON module can also be used from the command-line. It will both validate and pretty-print your JSON:

$ echo "{ \"name\": \"Monty\", \"age\": 45 }" | \
python3 -m json.tool
    "name": "Monty",
    "age": 45

You may also be interested in using the jq-tool for this though!

How to read a JSON file in python

Besides json.loads, there’s also a function called json.load (without the s). It will load data from a file. If you want to read the contents of a JSON file into Python and parse it, use the following example:

with open('data.json') as json_file:
    data = json.load(json_file)

How to write JSON to a file in Python

The json.dump function is used to write data to a JSON file.

data = {'name': 'Eric', 'age': 38 }

with open('data.json', 'w') as json_file:
    json.dump(data, json_file)

Frequently Asked Questions

How do I convert a list (array) to JSON in Python?

Simply use the methods described above. The json.dump and json.dumps functions accept both dictionaries and lists

How do I convert a dict or dictionary to JSON in Python?

Similar to arrays, so use json.dump or json.dumps on the dictionary.

How can I sort the JSON output in Python?

The dump and dumps functions both accept an option called sort_keys, for example: json.dumps(data, sort_keys=True).

Does the JSON library output Unicode data?

By default: no. The library outputs ASCII, and will convert characters that are not part of ASCII. If you want to output Unicode, set ensure_ascii to False. Example: json.dumps(data, ensure_ascii=False)

Keep learning

  • If you’re looking for a format that is easy to write for humans (e.g.: config files), read our article on reading and writing YAML with Python.
  • JMESPath is a query language for JSON. JMESPath in Python allows you to obtain the data you need from a JSON document or dictionary easily.
  • If you need to parse JSON on the command-line, try our article on a tool called jq!

Please help us and share this article

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!