2026-03-04•2 min read•by DevUtilz
JSON Minify vs Pretty Print
JSONDevelopmentToolsTutorial
JSON Minify vs Pretty Print
JSON can be formatted in two ways: minified (compressed) or pretty-printed (formatted).
Pretty Print (Formatted)
const data = { name: 'John', age: 30, city: 'NYC' };
// Pretty print with 2-space indentation
const pretty = JSON.stringify(data, null, 2);
// Output:
// {
// "name": "John",
// "age": 30,
// "city": "NYC"
// }
Minify (Compressed)
const data = { name: 'John', age: 30, city: 'NYC' };
// Minify - remove all whitespace
const minified = JSON.stringify(data);
// Output:
// {"name":"John","age":30,"city":"NYC"}
When to Use Each
Use Pretty Print When:
- Reading/debugging in console
- Writing to log files
- Displaying to users
- Code reviews
- Configuration files
Use Minify When:
- Sending over network (smaller size)
- Storing in databases
- API responses for bandwidth
- Caching
Size Comparison
const data = {
users: [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' }
]
};
const pretty = JSON.stringify(data, null, 2);
const minified = JSON.stringify(data);
console.log(`Pretty: ${pretty.length} bytes`);
console.log(`Minified: ${minified.length} bytes`);
console.log(`Savings: ${((pretty.length - minified.length) / pretty.length * 100).toFixed(1)}%`);
Real-world Example
API Response - Pretty (Development)
{
"status": "success",
"data": {
"id": 123,
"name": "Product Name",
"price": 29.99
}
}
API Response - Minified (Production)
{"status":"success","data":{"id":123,"name":"Product Name","price":29.99}}
Implementation
function formatJSON(json, pretty = true) {
if (typeof json === 'string') {
json = JSON.parse(json);
}
return pretty ? JSON.stringify(json, null, 2) : JSON.stringify(json);
}
Tools
# Pretty print
echo '{"a":1}' | python -m json.tool
# Minify
echo '{ "a": 1 }' | python -c "import sys,json; print(json.dumps(json.load(sys.stdin)))"
Conclusion
Use pretty print for readability during development. Use minified for production to save bandwidth. Consider content negotiation to serve appropriate format.