How to Minify JSON: A Practical Guide to Smaller Files and Faster APIs
Your API response is 500 KB. After gzip, it is 45 KB. So why bother minifying? Because gzip and minification are not the same thing. One shrinks bytes for the wire. The other shrinks bytes for the parser.
What Minification Actually Does
Take this prettified JSON:
{
"users": [
{
"id": 1,
"name": "Alice Chen",
"email": "alice@example.com",
"roles": [
"admin",
"editor"
]
},
{
"id": 2,
"name": "Bob Martinez",
"email": "bob@example.com",
"roles": [
"viewer"
]
}
]
}After minification, it becomes:
{"users":[{"id":1,"name":"Alice Chen","email":"alice@example.com","roles":["admin","editor"]},{"id":2,"name":"Bob Martinez","email":"bob@example.com","roles":["viewer"]}]}The data is identical. Every key, every value, every structural relationship is preserved. Only the formatting whitespace is removed.
Why Minification Matters
Three reasons, in order of practical impact:
Bandwidth. Every byte you send over the network costs time and money. API responses, static JSON files served by CDNs, configuration payloads bundled into apps - minification shrinks all of them. For mobile users on slow connections, the difference is noticeable.
Storage.If you're storing JSON documents in a database or log files, whitespace adds up. A million prettified log entries take significantly more disk space than minified ones.
Parse speed.Parsing fewer bytes is faster. The difference per document is tiny, but when you're parsing thousands of JSON documents per second - a logging pipeline, a message queue consumer - it adds up.
How Much Space Do You Save?
The savings depend on the nesting depth and how the original file was formatted. Some real-world numbers:
- A typical API response (moderate nesting, 2-space indent): 15-25% reduction
- A deeply nested configuration file (4-space indent): 25-40% reduction
- A large GeoJSON file with coordinate arrays: 30-50% reduction
Using Online Tools
The fastest way to minify a one-off JSON file is to paste it into an online tool. JSON Prettifier handles both directions - format messy JSON for readability or minify it for production. Everything runs in your browser, so your data never leaves your machine.
Command-Line Methods
For batch processing or scripting, the command line is more practical.
Using jq
jq is the Swiss army knife for JSON on the command line. The -c flag outputs compact (minified) JSON:
# Minify a file
jq -c '.' input.json > output.json
# Minify inline
echo '{"name": "Alice", "age": 30}' | jq -c '.'
# {"name":"Alice","age":30}Using Python
Python's json module is available everywhere, no installation needed:
# One-liner: minify a JSON file
python -c "import json,sys; json.dump(json.load(open(sys.argv[1])),sys.stdout,separators=(',',':'))" input.jsonThe key is the separators=(',', ':') argument. By default, Python's json.dump() adds a space after commas and colons. Setting these separators explicitly removes those trailing spaces.
JavaScript Methods
In JavaScript, minification is just JSON.stringify() without the third argument:
// Prettified (the third argument adds indentation)
const pretty = JSON.stringify(data, null, 2);
// Minified (no third argument = no whitespace)
const minified = JSON.stringify(data);If you have a prettified JSON string and want to minify it without changing any data, parse and re-stringify:
function minifyJson(jsonString) {
return JSON.stringify(JSON.parse(jsonString));
}
// In Node.js, for files:
const fs = require("fs");
const input = fs.readFileSync("data.json", "utf-8");
fs.writeFileSync("data.min.json", minifyJson(input));Build Step Automation
If you're shipping JSON files as part of a web application, minify them during the build process rather than manually.
Webpack
With webpack, you can use copy-webpack-plugin with a transform function:
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{
from: "src/data/*.json",
to: "data/[name][ext]",
transform(content) {
return JSON.stringify(JSON.parse(content.toString()));
}
}
]
})
]
};npm script
The simplest approach - just add a script to package.json:
{
"scripts": {
"minify-json": "for f in src/data/*.json; do jq -c '.' \"$f\" > \"dist/data/$(basename $f)\"; done"
}
}Minification vs. Compression
Minification and compression are different techniques that complement each other. Here's where it gets interesting:
Example: 100 KB API response
Prettified: 100 KB
Prettified + gzip: 12 KB
Minified: 75 KB
Minified + gzip: 11 KBThe minified+gzipped version is only about 8% smaller than the prettified+gzipped version. So if your server already sends compressed responses, minification provides a smaller marginal benefit.
But minification still helps in three ways even with compression:
- Faster compression. Smaller input means the compression algorithm runs faster, reducing CPU time on the server.
- Faster parsing. After decompression on the client, the parser has fewer bytes to process.
- Contexts without compression. Not every transport layer supports compression - WebSocket messages, localStorage, embedded device protocols, and some CDN configurations serve content uncompressed.
When NOT to Minify
- Configuration files checked into version control. Keep these prettified. Developers need to read and review diffs on them. Minified config files make pull requests unreadable.
- During development and debugging.You need to inspect API responses, test fixtures, and mock data. Keep things readable while you're working.
- Small payloads.If your JSON response is 200 bytes, the 30 bytes you save from minification don't justify the added build complexity.
- Log files meant for human consumption. If someone needs to tail your logs and make sense of JSON payloads, prettified is better.
Real Numbers: A Benchmark
Testing with a realistic 5 MB JSON file (an array of 10,000 user records with nested addresses and order histories):
- Prettified (2-space indent): 5.0 MB
- Minified: 3.8 MB (24% smaller)
- Prettified + gzip: 285 KB
- Minified + gzip: 262 KB (8% smaller than prettified+gzip)
- Minified + Brotli: 218 KB (best overall)
Parse time (Node.js, averaged over 100 runs): JSON.parse(prettified) took 48ms, JSON.parse(minified)took 38ms. That 21% improvement matters when you're handling hundreds of requests per second.
For the reverse operation - making minified JSON readable again - see our guide on how to format JSON. And if you're optimizing API payloads specifically, our JSON in REST APIs guide covers structuring responses for performance.
Minify or Format - Your Choice
Need to minify or format JSON right now? Our browser-based tool handles both directions - paste, click, done.
Open JSON Prettifier