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
import json
data = {"name": "John", "age": 30, "roles": ["admin", "user"]}
# indent=2 adds readable indentation
formatted = json.dumps(data, indent=2)
print(formatted)import json
formatted = json.dumps(
data,
indent=4,
sort_keys=True,
ensure_ascii=False, # keep accented characters as-is
)
print(formatted)# 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.toolPrefer 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
Use json.dumps(data, indent=2). The indent argument controls how many spaces are used per nesting level, producing readable, indented output.
By default json.dumps escapes non-ASCII characters. Setting ensure_ascii=False keeps characters like accents and emoji readable in the output.
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.
JSON Formatter
Format and beautify JSON with proper indentation.
JSON Validator
Validate JSON syntax with detailed error messages.
JSON Minifier
Compress JSON by removing whitespace to reduce size.
JSON Beautifier
Turn minified JSON into clean, readable code.
CSV to JSON
Convert CSV data into JSON for APIs and apps.
Learn More About JSON
All JSON syntax rules and data types on one page.
View cheat sheetFix the 10 most frequent JSON syntax mistakes.
Read guide