Python Guide

JSON Formatter for Python

Learn how to format and pretty print JSON in Python using the built-in json module — or paste your data into our free online formatter for a one-off job.

Format JSON in Python: Code Examples

Pretty print with json.dumps()
import json

data = {"name": "John", "age": 30, "roles": ["admin", "user"]}

# indent=2 adds readable indentation
formatted = json.dumps(data, indent=2)
print(formatted)
Sort keys and keep Unicode readable
import json

formatted = json.dumps(
    data,
    indent=4,
    sort_keys=True,
    ensure_ascii=False,  # keep accented characters as-is
)
print(formatted)
Format a JSON file from the command line
# Python ships with a built-in JSON tool
python -m json.tool input.json output.json

# Or pretty print to the terminal
cat input.json | python -m json.tool

Prefer a quick online tool?

You don't need to write code to format JSON once. Paste it into our free formatter and get clean, indented output instantly — nothing leaves your browser.

Python JSON FAQ

How do I pretty print JSON in Python?

Use json.dumps(data, indent=2). The indent argument controls how many spaces are used per nesting level, producing readable, indented output.

What does ensure_ascii=False do?

By default json.dumps escapes non-ASCII characters. Setting ensure_ascii=False keeps characters like accents and emoji readable in the output.

How can I sort JSON keys in Python?

Pass sort_keys=True to json.dumps() to output object keys in alphabetical order, which is useful for diffing and consistent files.

Free JSON Tools You Can Use Right Now

Put these tips into practice with our free online JSON tools. No signup, everything runs in your browser.

Learn More About JSON

JSON Cheat Sheet

All JSON syntax rules and data types on one page.

View cheat sheet
Common JSON Errors

Fix the 10 most frequent JSON syntax mistakes.

Read guide