~/guides/-guides-minify-json-in-code-
guides · JSON

How to Minify JSON in JavaScript, Python, jq, Go and PHP

Copy-paste ways to minify JSON in code and on the command line, plus the gotchas: Python default separators, PHP escaped slashes, and large numbers losing precision.

last updated · November 6, 2026by @vultio

The one rule: parse, then serialize compactly

Minifying JSON means removing the whitespace between tokens — indentation, line breaks and the spaces after : and , — without touching the data. Every reliable method does it the same way: parse the text into a value, then write it back out without formatting.

Never minify JSON with a regular expression that deletes spaces or newlines. Whitespace inside string values is data: "first name" must stay "first name", and a \n inside a string is an escape sequence, not a line break.

JavaScript and Node.js

In code
const minified = JSON.stringify(JSON.parse(text));
Command line with Node
node -e "process.stdout.write(JSON.stringify(JSON.parse(require('fs').readFileSync(0, 'utf8'))))" < input.json > output.json

If you already have an object, JSON.stringify(obj) with no third argument is minified output. Parsing first is only needed when you start from text.

Python: watch the default separators

json.dumps is not fully minified by default. Without an indent it still writes a space after every comma and colon:

import json

json.dumps({"a": 1, "b": [1, 2]})
# '{"a": 1, "b": [1, 2]}'   <- spaces remain

json.dumps({"a": 1, "b": [1, 2]}, separators=(",", ":"))
# '{"a":1,"b":[1,2]}'

Add ensure_ascii=False if you want accented letters and emoji written as-is instead of é-style escapes, which are six bytes each.

Command line (Python 3.9+)
python -m json.tool --compact input.json output.json

jq

jq -c . input.json > output.json

# an array of records -> one compact object per line (JSON Lines)
jq -c '.[]' records.json > records.jsonl

-c (compact output) is the whole trick. The second form is useful for log pipelines and bulk-load tools that expect one JSON document per line.

Go

var buf bytes.Buffer
if err := json.Compact(&buf, data); err != nil {
    return err // data was not valid JSON
}
minified := buf.Bytes()

json.Compact works directly on the bytes: it validates the input and removes insignificant whitespace without decoding into Go values, so key order and number formatting are preserved exactly.

PHP

$minified = json_encode(
    json_decode($json),
    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
);

Without the flags, PHP escapes every / as \/ and every non-ASCII character as a \uXXXX sequence, so the "minified" output can end up larger than the input. Both forms are valid JSON, but they make URLs and text harder to read and diff.

The big-number gotcha

Parsing and re-serializing can change numbers. JavaScript stores every number as a 64-bit float, so integers above 2^53 (9007199254740992) lose precision:

JSON.stringify(JSON.parse('{"id":12345678901234567890}'));
// '{"id":12345678901234567000}'   <- a different id
MethodLarge integers
JSON.parse + JSON.stringify (JavaScript)Can change values above 2^53
json.loads + json.dumps (Python)Preserved (Python integers are arbitrary precision)
json.Compact (Go)Preserved (no decoding)
jq 1.7 and laterPreserved for numbers it does not modify
jq 1.6 and earlierConverted to double, can change values

If your data contains 64-bit IDs (Twitter/X, Discord and many databases use them), minify with a method that does not round-trip through floats, or make sure those IDs are sent as strings.

Is minifying worth it?

  • For HTTP responses that are already gzip- or brotli-compressed, minifying typically saves much less than the raw byte difference suggests, because whitespace compresses very well.
  • It still helps for JSON stored uncompressed: localStorage, database columns, cache entries, message queues and embedded <script type="application/json"> blocks.
  • Keep the formatted version where humans work: committed config files, documentation, and fixtures that appear in code review.

For a quick one-off, paste the JSON into the JSON Minifier; for repeatable builds, use one of the commands above in a script so the output is the same every time.