Data•7 min read
JSON Beautify and Minify: Format JSON Data Like a Pro
Learn to format and compress JSON data. Master JSON beautification for readability and minification for performance.
By Andy Pham
JSON Beautify and Minify: Format JSON Data Like a Pro
JSON formatting is essential for development and production. Learn when to beautify for readability and when to minify for performance.
JSON Beautification
Before and After
// Before (minified)
{"name":"John","age":30,"address":{"city":"NYC","zip":"10001"},"hobbies":["reading","coding"]}
// After (beautified)
{
"name": "John",
"age": 30,
"address": {
"city": "NYC",
"zip": "10001"
},
"hobbies": [
"reading",
"coding"
]
}
JavaScript Beautification
const data = { name: "John", age: 30 };
// Beautify with 2-space indent
const beautified = JSON.stringify(data, null, 2);
// Beautify with 4-space indent
const beautified4 = JSON.stringify(data, null, 4);
// Beautify with tabs
const beautifiedTab = JSON.stringify(data, null, ' ');
Python Beautification
import json
data = {"name": "John", "age": 30}
# Beautify
beautified = json.dumps(data, indent=2)
# With sorted keys
beautified_sorted = json.dumps(data, indent=2, sort_keys=True)
JSON Minification
Size Comparison
| Format | Size | Reduction |
|---|---|---|
| Beautified (2-space) | 156 bytes | - |
| Beautified (4-space) | 208 bytes | - |
| Minified | 89 bytes | 43% |
JavaScript Minification
const data = { name: "John", age: 30 };
// Minify
const minified = JSON.stringify(data);
// {"name":"John","age":30}
Advanced Minification
// Remove unnecessary precision
function minifyJson(json) {
const obj = JSON.parse(json);
return JSON.stringify(obj);
}
// This automatically:
// - Removes extra whitespace
// - Removes trailing zeros from numbers
// - Uses shortest escape sequences
Use Cases
Beautify When:
- Debugging API responses
- Code review
- Documentation
- Config files in repos
- Learning/teaching
Minify When:
- API responses
- Storing in databases
- Network transfer
- Production configs
- LocalStorage/cookies
Best Practices
- Use consistent indentation (2 spaces is standard)
- Minify in production
- Keep source files beautified
- Use build tools for automatic minification
Try Our JSON Tools
- JSON Beautifier - Format JSON
- JSON Minifier - Compress JSON
- JSON Validator - Validate JSON
Conclusion
Beautify JSON for development readability, minify for production performance. Most build tools handle this automatically.