Minify JSON to one line: what changes, what it saves and when to skip it

By TechCompare · Updated · How we check

Minify JSON when a program is the only reader, such as an API response, a database column or a queue message.

Minifying JSON takes out the spacing so a whole document fits on one line. This page shows how much a sample shrinks, why one record per line suits logs, and when a file on one line is the wrong choice. To get the text on one line, paste your JSON into the formatter and press Minify.

Topic
Minify JSON
json-minify
Category
Best Practices
JSON knowledge base

How this is calculated

Minifying removes the whitespace between JSON tokens. RFC 8259 allows four whitespace characters between tokens: space, tab, line feed and carriage return. Minifying takes out the indentation, the line breaks and the space after each colon. Keys, values and key order stay exactly as they were, and whitespace inside a string stays too, since it's part of the value. The formatted sample comes first, then the Minify output.

Formatted
{
  "order": {
    "id": "A-1042",
    "customer": {
      "name": "Ada Lovelace",
      "email": "ada@example.com"
    },
    "items": [
      {
        "sku": "KB-01",
        "qty": 2,
        "price": 49.9
      },
      {
        "sku": "MS-07",
        "qty": 1,
        "price": 19.5
      }
    ],
    "paid": true,
    "note": null
  }
}
Minified
{"order":{"id":"A-1042","customer":{"name":"Ada Lovelace","email":"ada@example.com"},"items":[{"sku":"KB-01","qty":2,"price":49.9},{"sku":"MS-07","qty":1,"price":19.5}],"paid":true,"note":null}}

The formatted sample is 346 bytes and the minified one is 194 bytes. That's 152 bytes less, or 44% of the formatted text. It's one small sample, so treat the percentage as an example, not a rule. Deeper nesting means more indentation on each line, and short values make that indentation a bigger share of the file. A document made mostly of long strings saves little, since minifying leaves string contents alone.

Browsers and servers negotiate compression with the Accept-Encoding and Content-Encoding headers, and MDN recommends it for every file except ones that are already compressed, like images, audio and video. gzip is the most common method, and br is the other one that matters. Repeated indentation compresses well. With gzip on, the gap between the formatted and minified text shrinks a lot. Your payloads will differ, so check the Content-Encoding header on a real response and measure both versions before you decide whether minifying is worth the work.

Logs are the clearest case for one record per line. JSON Lines puts one JSON value on each line, and the format's site says: "It's a great format for log files." A minified record can't hold a raw line break. RFC 8259 requires control characters to be escaped inside strings, so a line break inside a value is written as the two characters \n, and minifying removes the breaks between tokens. Each record fits on one line, so grep, tail and wc -l see whole records.

Any value that has to sit on one line, like an environment variable, is a good place for minified JSON, since a single line has no room for indentation.

Minifying needs valid JSON. A trailing comma, a single quote or a comment (unless Allow comments is on) makes the tool show an error instead of any output, so use the parse errors guide to find the mistake. Numbers come through untouched too. 1.10 stays 1.10, and a 16-digit ID like 9007199254740993 comes out exactly as written, because the tool reads each number by its written digits. The large numbers page explains why that matters for IDs.

Verdict

Keep a config file people edit in its formatted form, and minify it only when you ship it. Minifying a file with comments drops the comments, so keep the original (see JSON with comments). Minifying also isn't protection, since anyone can format the text again in a second. For debugging and review, the formatted copy is the one to use, as the JSON prettify guide explains. Measure a real payload before you count on the savings.

More JSON scenarios

Frequently asked questions

How much smaller does minified JSON get?
The sample on this page shrinks by 44%. That's one file, and the share depends on how much indentation it has. Measure your own file to get your number.
Does minifying change my data?
No. Only the whitespace between tokens goes. Keys, key order, values and number digits all stay as written, and so does whitespace inside a string.
Does gzip make minifying pointless?
Compression narrows the gap a lot, because it squeezes out repeated indentation, so the saving on the wire can be far smaller than the raw byte count. Whether minifying still pays off depends on your payloads, so compare a real response with compression on, formatted and minified.
How do I put JSON on one line for a log?
Minify each record on its own, since the formatter reads one JSON document at a time. Write each result on its own line, and save the file with the .jsonl extension. For a large log, a short script that does this for every line is more practical than pasting records one by one.
Can I minify JSON with comments?
Yes, with Allow comments turned on. The tool reads // and /* */ comments and leaves them out of the output, so the one-line text has none. Keep the original file, because the comments are gone once you minify it.