How to Minify JSON (and When You Shouldn't)
What minification does to a JSON file, how much it actually helps, and the situations where keeping JSON readable is the better call.
What JSON minification does
JSON minification removes all whitespace characters — spaces, tabs, and newlines — that exist outside of string values. The RFC 8259 spec defines these characters as insignificant when they appear between JSON tokens, which means removing them produces a string that any JSON parser reads identically to the formatted version.
Here is the same JSON object before and after minification:
// Before — 178 bytes (2-space indented)
{
"product": "API Gateway",
"version": "2.1.0",
"features": [
"rate limiting",
"auth",
"logging"
],
"active": true
}
// After — 82 bytes (54% smaller)
{"product":"API Gateway","version":"2.1.0","features":["rate limiting","auth","logging"],"active":true}
The 96-byte difference in this example is entirely whitespace — newlines, spaces, and the two-space indentation. Both strings parse to the exact same JavaScript object or Python dictionary.
How much size does minification actually save?
The savings depend on two factors: indentation depth and nesting level. The deeper and more heavily indented the JSON, the more whitespace it contains, and the more minification saves.
- Shallow, 2-space indented JSON: ~15–25% smaller after minification.
- Moderate nesting, 2-space: ~25–40% smaller.
- Deep nesting (6+ levels), 4-space: 40–60% smaller.
In practice, an API response or database export often lands in the 25–40% range. That's meaningful: a 100KB response becomes 60–75KB, reducing both transfer time and parse time on constrained devices.
When to minify JSON
Minification is worthwhile in these situations:
- API responses. JSON served over HTTP benefits from being as small as possible, especially for mobile clients on metered connections.
- Data stored in a database as JSON strings. Some databases charge for storage by byte count — minifying reduces cost.
- Message queues. Kafka, SQS, and similar systems often have per-message size limits. Minifying gives you more room for actual data.
- IoT and embedded devices. Devices with limited RAM need smaller payloads to parse without running out of memory.
- Bundled JSON in a web app. JSON files included in a JavaScript bundle should be minified the same as the JS code.
When NOT to minify JSON
Minification trades readability for size. In some contexts, readability wins:
- Config files edited by humans.
package.json, VS Code settings, linter configs — these are maintained by developers and need to be readable. Minifying them causes pain every time someone needs to edit them. - Files in version control. Minified JSON produces noisy diffs. Adding one key produces a single-line change that modifies the entire file in one line, making code review painful.
- Debug and development builds. Keep JSON formatted in dev and staging. Minify in production only, so that developers can read logs and responses without a formatter.
- JSON used as a source of truth for documentation or sharing. If others will read the JSON directly, keep it formatted.
How to minify JSON — four ways
1. Online (fastest for one-off use)
Go to json-indent.com/minifier.html, paste your formatted JSON, and the minified result appears immediately. Copy or download the output.
2. Python
import json
# From a string
data = json.loads(formatted_json_string)
minified = json.dumps(data, separators=(',', ':'))
# From a file
with open('data.json') as f:
data = json.load(f)
with open('data.min.json', 'w') as f:
json.dump(data, f, separators=(',', ':'))
The separators=(',', ':') argument removes the default space after commas and colons in json.dumps output.
3. JavaScript / Node.js
// JSON.stringify without the third argument produces minified output
const minified = JSON.stringify(JSON.parse(formattedJsonString));
// From a file in Node.js
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
fs.writeFileSync('data.min.json', JSON.stringify(data));
4. Command line with jq
# The -c flag produces compact (minified) output
jq -c '.' data.json > data.min.json
# Pipe from curl
curl https://api.example.com/data | jq -c '.' > response.min.json
Minification vs compression: what's the difference?
Minification and compression are separate operations that work at different levels and compound each other:
- Minification removes whitespace at the text level, producing a smaller JSON string.
- Compression (gzip, Brotli, deflate) encodes the text using algorithms that exploit repetition, further reducing the byte count for transfer.
Most web servers and CDNs apply gzip or Brotli automatically for text content types including application/json. Both together beat either alone. A 100KB formatted JSON file might compress to 25KB with gzip, or compress+minify to 18KB. The gains are real and additive.
Minify JSON in your browser — free
Paste formatted JSON and get the minified result instantly. Shows the original size, minified size, and savings percentage.
Open JSON Minifier →Frequently Asked Questions
What does minifying JSON do?
Minifying JSON removes all whitespace characters (spaces, tabs, newlines) that exist outside of string values, producing the smallest valid JSON string. The data is identical — only formatting whitespace is removed.
How much smaller does JSON get after minification?
Typically 20–50% smaller, depending on the original indentation depth and nesting level. A 4-space indented JSON file with many nested objects can shrink by half. A shallow, 2-space indented file may only shrink by 20%.
Is minified JSON still valid JSON?
Yes. Whitespace outside string values is insignificant in JSON (RFC 8259). Minified JSON is 100% valid and parseable by any JSON parser. The data is unchanged.
What is the difference between JSON minification and compression?
JSON minification removes whitespace at the text level. Compression (gzip, Brotli, deflate) is a separate operation at the transfer level that reduces bytes using algorithms that exploit repetition. Both compound: minify first, then let gzip compress further. Most web servers apply gzip automatically to application/json responses.