JSON minification and formatting are opposites — but both are essential skills. This guide explains when to use each and how to do both in JavaScript and Python.
What is JSON formatting (beautifying)?
Formatting adds indentation and line breaks to make JSON human-readable:
// Formatted / beautified
{
"name": "Alice",
"age": 28,
"tags": [
"developer",
"javascript"
]
}
What is JSON minification?
Minification removes all whitespace to reduce size:
// Minified
{"name":"Alice","age":28,"tags":["developer","javascript"]}
Size difference
The formatted version above is 89 characters. The minified version is 57 characters — a 36% reduction. For large API responses, this adds up significantly.
When to format
- Debugging API responses in your browser or terminal
- Code review and git diffs
- Configuration files humans edit
- Documentation and examples
- Storing JSON in version control
When to minify
- API responses sent to clients
- JSON stored in databases (saves space)
- Static JSON files served by a CDN
- JSON embedded in HTML pages
- Any performance-critical data transfer
Do both in code
// JavaScript
const obj = { name: 'Alice', age: 28 };
// Format with 2-space indent
const formatted = JSON.stringify(obj, null, 2);
// Minify
const minified = JSON.stringify(obj);
// Format existing minified JSON
const reformatted = JSON.stringify(JSON.parse(minifiedString), null, 2);
# Python
import json
# Format
formatted = json.dumps(obj, indent=2)
# Minify (no spaces after , and :)
minified = json.dumps(obj, separators=(',', ':'))
# Reformat a minified file
with open('minified.json') as f:
data = json.load(f)
with open('formatted.json', 'w') as f:
json.dump(data, f, indent=2)Try it free — JSON Minifier
Minify JSON instantly and see exact size savings.