6 Tricks to Effectively Use JSON in Python

JSON in Python is easy; get up and running quickly

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. 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. Most of what follows assumes you imported the JSON library.

1. 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:

>>> 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

2. 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

3. 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!

4. 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)

5. 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)

6. Searching through JSON with JMESPath

Screenshot by author

JMESPath is a query language for JSON. It allows you to easily obtain the data you need from a JSON document. If you ever worked with JSON before, you probably know that it’s easy to get a nested value.

For example: doc["person"]["age"] will get you the nested value for age in a document that looks like this:

"persons": {
"name": "erik",
"age": "38"

But what if you want to extract all the age-fields from an array of persons, in a document like this:

"persons": [
{ "name": "erik", "age": 38 },
{ "name": "john", "age": 45 },
{ "name": "rob", "age": 14 }

We could write a simple loop and loop over all the persons. Easy peasy. But loops are slow and introduce complexity to your code. This is where JMESPath comes in!

This JMESPath expression will get the job done:


It will return an array with all the ages: [38, 45, 14].

Say you want to filter the list, and only get the ages for people named ‘erik’. You can do so with a filter:


See how natural and quick this is?

JMESPath is not part of the Python standard library, meaning you’ll need to install it with pip or pipenv. For example, when using pip in in virtual environment:

$ pip3 install jmespath
$ python3
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
>>> import jmespath
>>> j = { "people": [{ "name": "erik", "age": 38 }] }
>>> jmespath.search("people[*].age", j)

You’re now ready to start experimenting! Make sure to try the interactive tutorial and view the examples on the JMESPath site!

Software developer by day, writer at night. Webmaster at https://python.land

Sign up for Python Land Newsletter

By Python Land

Get updated on our best articles. No spam. No selling of your data. Promised! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store