JSON in Python: How To Read, Write, and Parse

JSON, short for JavaScript Object Notation, is an open standard. Although its name doesn’t imply so, it is a language-independent data format. With Python’s JSON library, we can read, write, and parse JSON to both store and exchange data using this versatile data format. It’s a prevalent data format because it is easy to read and write for humans as well, 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. In this article, I’ll show you how to use the built-in Python JSON library. In addition, we’ll take a look at JSON5: an extension to JSON that allows things like comments inside your JSON documents.

Importing the built-in JSON library

Python ships with a powerful and elegant JSON library to help you decode and encode JSON. You can import the module with:

import jsonCode language: Python (python)

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}'
>>> person = json.loads(jsonstring)
>>> print(person['name'], 'is', person['age'], 'years old')
erik is 38 years old
>>> print(person)
{'name': 'erik', 'age': 38, 'married': True}Code language: Python (python)

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. You can check for yourself:

>>> type(person)
<class 'dict'>Code language: Python (python)

Encoding JSON with json.dumps

Encoding JSON data with Python’s json.dumps 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:

import json

person = {'name': 'erik', 'age': 38, 'married': True}
json_string = json.dumps(person)
print(json_string)
# {"name": "erik", "age": 38, "married": true}

# To make sure, let's print the type too
print(type(json_string))
# <class 'str'>Code language: Python (python)

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:

>>> person = {'name': 'erik', 'age': 38, 'married': True}
>>> print(json.dumps(person, indent=2))
{
  "name": "erik",
  "age": 38,
  "married": true
}Code language: Python (python)

Pretty printing JSON on the command line

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
}Code language: Python (python)

Since Python 3.14, you can also type the shorter python3 -m json, which does the same thing and adds colors to the output when you run it in a terminal:

$ echo '{ "name": "Monty", "age": 45 }' | python3 -m json
{
    "name": "Monty",
    "age": 45
}Code language: Python (python)

If the input isn’t valid JSON, both commands tell you what’s wrong and where:

$ echo '{"name": "Monty",}' | python3 -m json
Illegal trailing comma before end of object: line 1 column 17 (char 16)Code language: Python (python)

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, but you have to open the file yourself. 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)
    ...
Code language: Python (python)

How to write JSON to a file in Python

The json.dump function is used to write data to a JSON file. You’ll need to open the file in write mode first:

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

with open('data.json', 'w') as json_file:
    json.dump(data, json_file)
Code language: Python (python)

Handling invalid JSON

JSON often comes from somewhere you don’t control: a file someone edited by hand, or an API that returned an error page instead of data. When json.loads or json.load gets invalid JSON, it raises a json.JSONDecodeError. You can catch it with a try-except block:

import json

bad_json = '{"name": "Monty", "age": 45,}'

try:
    data = json.loads(bad_json)
except json.JSONDecodeError as e:
    print(f"Invalid JSON: {e}")Code language: Python (python)

The output tells you exactly what’s wrong and where:

Invalid JSON: Illegal trailing comma before end of object: line 1 column 28 (char 27)Code language: Python (python)

Converting types that JSON doesn’t support

JSON only knows about objects, arrays, strings, numbers, booleans, and null. Python’s json module converts dictionaries, lists, tuples, strings, integers, floats, True, False, and None for you. Anything else, like a date, a Decimal, or your own objects, gives an error:

import json
from datetime import date

order = {'item': 'book', 'shipped': date(2026, 11, 10)}
print(json.dumps(order))
# TypeError: Object of type date is not JSON serializable
# when serializing dict item 'shipped'Code language: Python (python)

The default argument fixes this. It takes a function that Python calls for every object it can’t convert. The function should return something that JSON can handle. Often, str is all you need:

import json
from datetime import date
from decimal import Decimal

order = {'item': 'book', 'price': Decimal('19.95'), 'shipped': date(2026, 11, 10)}
print(json.dumps(order, default=str))
# {"item": "book", "price": "19.95", "shipped": "2026-11-10"}Code language: Python (python)

Note that the date and the price are now strings. When you load this JSON again, you get strings back, not a date and a Decimal. You have to convert those yourself.

For a dataclass, convert it to a dictionary first with asdict():

import json
from dataclasses import dataclass, asdict

@dataclass
class Person:
    name: str
    age: int

monty = Person('Monty', 45)
print(json.dumps(asdict(monty)))
# {"name": "Monty", "age": 45}Code language: Python (python)

JSON5 vs. JSON

JSON5 is an extension of JSON. The main advantage of JSON5 over JSON is that it allows for more human-readable and editable JSON files. Notable JSON5 features are:

  • single-line and multi-line comments
  • trailing commas in objects and arrays
  • single-quoted strings

For machine-to-machine communication, I recommend using the built-in JSON library. However, when using JSON as a configuration file, JSON5 is recommended, mainly because it allows for comments.

Python does not support JSON5 natively. To read and write JSON5, we’ll need to pip install one of the following packages:

  • PyJSON5: a library that uses the official JSON5 C library, making it the fastest option to use.
  • json5: a pure Python implementation, confusingly called pyjson5 as well on in their documentation. According to the author, the library is slow.

I recommend the first (fast) option, but unless you are parsing hundreds or thousands of documents, the speed advantage will be negligible.

Both libraries offer functions that mimic the Python JSON module, making it super easy to convert your code to JSON5. You could, for example, do an import pyjson5 as json but I recommend making it more explicit that you’re using json5 as show in the following example:

import pyjson5 as json5

with open("person.json") as f:
    p = json5.decode(f.read())

print(p)
{
    "name": "erik",
    "age": 25,
    // The adress is optional
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY",
        "zip": "10001"
    }
}

To make it extra clear that you’re using JSON5, you can also use the extension .json5. While you’re at it, search the marketplace of your code editor for a JSON5 plugin. E.g., VSCode has one or two.

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

Related articles

Leave a Comment