How to Pretty Print JSON in Python

Pretty printing JSON means formatting it with indentation and line breaks so it is easier to read.

This is useful when you want to:

  • inspect API data
  • read nested objects in the terminal
  • save cleaner JSON files
  • debug your program

In Python, the most common way to pretty print JSON is with the json module.

Quick fix

import json

data = {"name": "Ana", "age": 25, "skills": ["Python", "SQL"]}

print(json.dumps(data, indent=4))

Use json.dumps(..., indent=4) to print JSON with readable spacing and line breaks.

What pretty printing JSON means

Pretty printing does not change the actual data.

It only changes how the JSON looks when you print or save it.

Pretty printing usually adds:

  • indentation
  • line breaks
  • spacing between keys and values

This makes nested JSON much easier to read.

For example, compact JSON might look like this:

{"name":"Ana","age":25,"skills":["Python","SQL"]}

Pretty printed JSON looks like this:

{
    "name": "Ana",
    "age": 25,
    "skills": [
        "Python",
        "SQL"
    ]
}

Pretty print a Python dictionary as JSON

If your data is already a Python dictionary, use the json module and json.dumps().

  • Import the json module
  • Use json.dumps() to convert a Python object to a JSON string
  • Pass indent=4 for readable formatting
  • Use print() to display the result
import json

data = {
    "name": "Ana",
    "age": 25,
    "skills": ["Python", "SQL"]
}

formatted_json = json.dumps(data, indent=4)
print(formatted_json)

Output:

{
    "name": "Ana",
    "age": 25,
    "skills": [
        "Python",
        "SQL"
    ]
}

json.dumps() returns a string. If you want to learn more about converting dictionaries, see how to convert a dictionary to JSON in Python.

Pretty print JSON from a string

Sometimes JSON starts as a string, such as data from an API or a file.

In that case:

  1. Parse the JSON string with json.loads()
  2. Pretty print the result with json.dumps(..., indent=4)
import json

json_string = '{"name":"Ana","age":25,"skills":["Python","SQL"]}'

data = json.loads(json_string)
print(json.dumps(data, indent=4))

Output:

{
    "name": "Ana",
    "age": 25,
    "skills": [
        "Python",
        "SQL"
    ]
}

This is a common pattern when working with web data. For a full beginner guide, see how to parse JSON in Python.

Write pretty JSON to a file

When saving JSON to a file, use json.dump() instead of json.dumps().

The difference is:

  • json.dumps() returns a string
  • json.dump() writes directly to a file
import json

data = {
    "name": "Ana",
    "age": 25,
    "skills": ["Python", "SQL"]
}

with open("data.json", "w", encoding="utf-8") as file:
    json.dump(data, file, indent=4)

This creates a file named data.json with readable JSON inside.

This is useful for:

  • config files
  • exported data
  • saved API responses

Useful formatting options

You can control how the JSON looks with a few helpful options.

Choose the indentation size

Common choices are indent=2 and indent=4.

import json

data = {"name": "Ana", "age": 25}

print(json.dumps(data, indent=2))

Sort keys alphabetically

Use sort_keys=True if you want dictionary keys in alphabetical order.

import json

data = {"age": 25, "name": "Ana", "city": "Lima"}

print(json.dumps(data, indent=4, sort_keys=True))

Output:

{
    "age": 25,
    "city": "Lima",
    "name": "Ana"
}

Keep non-English characters readable

Use ensure_ascii=False if your data contains characters like é, ñ, or こんにちは.

import json

data = {"name": "José", "city": "Málaga"}

print(json.dumps(data, indent=4, ensure_ascii=False))

Output:

{
    "name": "José",
    "city": "Málaga"
}

These options improve readability, but they do not change the meaning of the JSON.

When pretty printing does not work

If pretty printing fails, one of these problems is usually the cause:

  • The data is not valid JSON
  • The Python object contains unsupported types
  • You mixed up json.dump() and json.dumps()
  • You are trying to format plain text that is not actually JSON

Example: invalid JSON string

This will fail because the JSON string is not valid:

import json

bad_json = "{name: 'Ana'}"
data = json.loads(bad_json)
print(json.dumps(data, indent=4))

Problems in that string:

  • keys must use double quotes
  • string values must use double quotes in JSON

Correct version:

import json

good_json = '{"name": "Ana"}'
data = json.loads(good_json)
print(json.dumps(data, indent=4))

Example: unsupported Python type

Some Python objects cannot be turned into JSON directly.

import json

data = {"numbers": {1, 2, 3}}

print(json.dumps(data, indent=4))

This raises an error because set is not a standard JSON type.

One fix is to convert the set to a list:

import json

data = {"numbers": list({1, 2, 3})}

print(json.dumps(data, indent=4))

Common mistakes

These are some common beginner mistakes when pretty printing JSON:

  • Using json.dumps() on invalid data and expecting Python to repair bad JSON automatically
  • Trying to pretty print unsupported Python types such as set
  • Forgetting to parse a JSON string first with json.loads() when needed
  • Using json.dump() without opening a file first
  • Assuming pretty printing changes the original dictionary

If you are not sure what kind of object you have, this can help:

print(type(data))

Useful commands while debugging:

python --version
python your_script.py
python -m json.tool data.json

You can also test formatting directly in your script:

import json

data = {"b": 2, "a": 1}
print(json.dumps(data, indent=4, sort_keys=True))

The python -m json.tool data.json command is especially useful for checking whether a JSON file is valid and formatted correctly.

FAQ

What is the easiest way to pretty print JSON in Python?

Use json.dumps(data, indent=4) and print the result.

What is the difference between json.dump() and json.dumps()?

json.dump() writes JSON to a file. json.dumps() returns JSON as a string.

Can I pretty print JSON from an API response?

Yes. Convert the response data to a Python object or parse the JSON string, then use json.dumps(..., indent=4).

Does pretty printing change the JSON data?

No. It only changes how the JSON looks.

How do I sort keys while pretty printing?

Use json.dumps(data, indent=4, sort_keys=True).

See also