How to Format JSON in Python – json.dumps with indent

Python's built-in json module makes it straightforward to format (pretty-print) JSON with a single parameter. You don't need any third-party libraries — everything you need is in the standard library.

Basic pretty-printing with json.dumps

The indent parameter is the key. Pass an integer to specify the number of spaces per indentation level:

import json

data = {"name": "Alice", "age": 28, "roles": ["admin", "editor"], "active": True}

# 2-space indentation (most common)
print(json.dumps(data, indent=2))

# Output:
# {
#   "name": "Alice",
#   "age": 28,
#   "roles": [
#     "admin",
#     "editor"
#   ],
#   "active": true
# }

Use indent=4 if your team's style guide calls for 4-space indentation. Note that Python booleans (True, False) are automatically converted to lowercase true/false as required by the JSON spec.

Sorting keys alphabetically

The sort_keys=True parameter outputs keys in alphabetical order, which makes JSON easier to compare and diff:

import json

data = {"zebra": 1, "apple": 2, "mango": 3}

print(json.dumps(data, indent=2, sort_keys=True))
# {
#   "apple": 2,
#   "mango": 3,
#   "zebra": 1
# }

Controlling separators for compact or readable output

The separators parameter controls the characters used between items and between keys and values. The default for pretty-printed output adds a space after colons and commas. For the most compact (minified) output:

import json

data = {"host": "localhost", "port": 5432}

# Default pretty output (separators=(',', ': '))
print(json.dumps(data, indent=2))

# Compact minified output — no spaces at all
print(json.dumps(data, separators=(',', ':')))
# {"host":"localhost","port":5432}

Reading and reformatting a JSON file

To reformat an existing JSON file — standardise indentation across a project, or convert minified JSON to readable JSON:

import json

# Read the file
with open('data.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# Write it back with 2-space indentation
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, indent=2, ensure_ascii=False)
    f.write('\n')  # Add trailing newline (good practice)

The ensure_ascii=False flag preserves Unicode characters (accented letters, emoji, etc.) rather than escaping them as \uXXXX sequences. This is almost always what you want for human-readable output.

Pretty-printing from the command line

Python ships with a command-line JSON formatter you can pipe into:

# Format a JSON file
python -m json.tool data.json

# Format JSON from an API response via curl
curl -s https://api.example.com/users | python -m json.tool

# Write formatted output to a file
python -m json.tool data.json > data-formatted.json

Using pprint for quick debug output

The pprint module pretty-prints Python data structures (dicts, lists) in a readable format. It's not JSON, but it's useful for quick debugging when you don't need valid JSON output:

from pprint import pprint
import json, urllib.request

with urllib.request.urlopen('https://api.example.com/data') as res:
    data = json.load(res)

pprint(data)  # Readable Python dict output for inspection

Use json.dumps(data, indent=2) when you need valid JSON. Use pprint(data) when you just want to inspect a Python dict quickly in a script.

Formatting JSON in the browser

If you're working with JSON responses from an API and want to format them without writing code, use the JSON Formatter. Paste any JSON — minified or broken — and get clean, indented output with a tree view. It's useful for inspecting API responses during development alongside your Python code.

Related articles