Format JSON in Python: Pretty Print, Indent, and Sort Keys
Python's built-in json module formats JSON with one argument: json.dumps(data, indent=2). For files and pipelines, the python -m json.tool command formats JSON from the terminal without writing any code.
Need to format a JSON snippet without writing code? Jsonic's JSON Formatter pretty prints and validates in one click.
Format JSON onlinejson.dumps with indent
The indent parameter of json.dumps controls indentation. Pass an integer for spaces, or a string (like '\t') for tabs.
import json
data = {
"user": {"name": "Alice", "role": "admin"},
"active": True,
"tags": ["api", "internal"]
}
# 2-space indent (most common)
print(json.dumps(data, indent=2))
# Output:
# {
# "user": {
# "name": "Alice",
# "role": "admin"
# },
# "active": true,
# "tags": [
# "api",
# "internal"
# ]
# }python -m json.tool (command line)
The json.tool module formats a JSON file or stdin output without writing a script. It is built into Python — no installation required.
# Format a file and print to stdout
python3 -m json.tool input.json
# Format a file and save the result
python3 -m json.tool input.json output.json
# Format JSON from a curl response
curl -s https://api.example.com/data | python3 -m json.tool
# Format JSON passed as a string
echo '{"name":"Alice","active":true}' | python3 -m json.tooljson.tool uses 4-space indentation by default. Pass --indent 2 to change it (Python 3.9+):
python3 -m json.tool --indent 2 input.jsonSort keys alphabetically
Pass sort_keys=True to sort all object keys alphabetically at every nesting level. Useful for consistent diffs and canonical output.
import json
data = {"z": 3, "a": 1, "m": 2}
print(json.dumps(data, indent=2, sort_keys=True))
# {
# "a": 1,
# "m": 2,
# "z": 3
# }From the command line: python3 -m json.tool --sort-keys input.json
Format and write to a file
import json
data = {"user": {"name": "Alice"}, "active": True}
with open("output.json", "w") as f:
json.dump(data, f, indent=2)
# Note: json.dump (not json.dumps) writes directly to a file objectUse json.dump (no trailing s) to write directly to a file object. Use json.dumps to get a formatted string.
Format a JSON API response
import json
import urllib.request
url = "https://api.example.com/users"
with urllib.request.urlopen(url) as response:
data = json.load(response)
# Pretty print the parsed response
print(json.dumps(data, indent=2))If you use the requests library, call response.json() first, then pass the result to json.dumps:
import json
import requests
response = requests.get("https://api.example.com/users")
data = response.json()
print(json.dumps(data, indent=2))Compact output (minify)
To produce the most compact JSON — no spaces after separators — pass separators=(',', ':'):
import json
data = {"name": "Alice", "active": True}
# Compact (minified)
print(json.dumps(data, separators=(',', ':')))
# {"name":"Alice","active":true}
# Default (slight spacing)
print(json.dumps(data))
# {"name": "Alice", "active": true}Frequently asked questions
How do I pretty print JSON in Python?
Use json.dumps(data, indent=2). The indent argument sets the number of spaces per nesting level. 2 and 4 are the most common values.
What is the difference between json.dump and json.dumps?
json.dumps returns a formatted string. json.dump writes directly to a file object. Both accept the same indent and sort_keys arguments.
How do I format a JSON file from the terminal?
Run python3 -m json.tool input.json. This reads the file, validates the JSON, and prints the formatted output to stdout. Redirect with > output.json to save it.
Does json.dumps preserve key order?
Yes. Since Python 3.7, dictionaries preserve insertion order, and json.dumps outputs keys in the same order unless you pass sort_keys=True.
Format JSON without writing code
For a quick paste-and-format without opening a terminal, Jsonic's JSON Formatter pretty prints, validates, and lets you copy the result in one step.
Open JSON Formatter