Places that want JSON on a single line
- The body of a curl -d request or an HTTP client field, pasted in one go
- An environment variable or a .env entry, where one value has to fit on one line
- Log files in JSON Lines (NDJSON), one record per line so tools can stream them
- A text column in a database or a key in browser storage that holds JSON as a string
Measured: one order, then two hundred
A single order record indented with two spaces, with an order number, a customer called 김민지 in 부산, two line items, a paid flag and a note, came to 291 bytes. Minified it was 184 bytes. All 107 bytes saved were indentation, line breaks and the space after each colon. The Korean names cost three bytes per character in UTF-8 before and after; minifying does not touch them.
Two hundred records of a similar shape in one array measured 19,745 bytes indented and 12,544 bytes minified, about 36% less. The shorter and more deeply nested the records, the larger the share of a pretty-printed file that is just indentation, so the saving grows.
Next to gzip and Brotli, the gap shrinks
Most web servers compress responses on the way out, and repeated spaces are exactly what compressors are good at. Compressing those 200 records with Node’s zlib at its default settings gave 2,320 bytes for the indented file and 1,965 for the minified one with gzip, and 1,279 against 1,222 with Brotli.
So for an API response that is sent compressed, stripping whitespace saved only a few hundred bytes in this sample, against more than 7,000 before compression. It still pays where no transfer compression steps in: a length limit on an environment variable, a log line, a database field, or a payload that some other system stores as is.
Building a JSON Lines file
JSON Lines is UTF-8 text with one valid JSON value per line, separated by \n. A record must not contain a raw line break, which minifying guarantees: a line break inside a string is already written as the two characters \n, so it stays inside the line. With one record per line, wc -l counts records, every line grep finds is a whole record, and appending a record means appending a line.
The tool takes one JSON value at a time. Paste two lines that are already JSON Lines, {"a":1} and then {"a":2}, and it reports a syntax error at line 2, column 1. Wrap several records in an array and you get one line, [{"a":1},{"a":2}]; for one record per line, minify each record on its own.
When to wrap the result as a string
Minifying {"msg": "say \"hi\"", "path": "C:\\temp"} gives {"msg":"say \"hi\"","path":"C:\\temp"}. With Wrap as a string, every quote and backslash is escaped once more and the whole thing is quoted: "{\"msg\":\"say \\\"hi\\\"\",\"path\":\"C:\\\\temp\"}". The doubled backslashes are correct, not a bug.
Use that form when JSON has to sit inside a string field of other JSON, or inside a string literal in JavaScript or Java source. For a shell command, use the unwrapped result inside single quotes instead; send the wrapped one with curl and the server receives a single string, not an object.