← All blog posts

JSON Minifier formula — how it actually works

WhiteUI · A plain-English guide to the JSON Minifier

Minifying JSON looks like deleting spaces, but the tool does something more careful than that: it reads your JSON properly, understands it as data, and then writes that data back out in the shortest legal form. That two-step method is why the result is always valid JSON, and why numbers come out rewritten. The whole method is one line, and it is written out below.

minified = JSON.stringify(JSON.parse(input))
Step 1: parse the input into data.
Step 2: write the data back with no spaces.
Nothing inside a string value is changed.

At a glance

Profile: 17 of 81 bytes savedSaved 17 bytesKept 64 bytes81 → 64 bytes · 21.0% smaller
Minifying a small profile saves 17 of 81 bytes: the result is 64 bytes, which is 21.0 percent smaller.

Step 1: parse the input into data

The first half of the line, JSON.parse(input), reads your text and turns it into real data: objects, lists, strings, numbers, and true and false values. This step is also the check: if your text has a mistake, such as a comma after the last item or a single quote where a double quote belongs, parsing fails and the tool tells you the JSON is invalid instead of giving you a wrong answer. Parsing is strict, which is what makes the tool trustworthy: it will not silently pass on a file it could not read.

Step 2: write the data back with no spaces

The second half, JSON.stringify, takes that data and writes it out again, this time with no line breaks, no indenting and no spaces between items. The result is a single line that holds exactly the same data as the input. For the small profile in the worked example below, the input is 81 bytes and the output is 64 bytes, so 17 bytes are saved. The saving comes only from the spacing: every space that separated two pieces of data is gone, and every line break is gone.

Numbers are rewritten in their shortest form

Because the tool works with data rather than text, numbers come out written differently. A price typed as 149.50 is read as the number 149.5, and written back as 149.5. A total typed as 597.00 is read as 597, and written back as 597. The values are identical: 149.50 and 149.5 are the same number, and so are 597.00 and 597. Only the writing changes, and the shorter writing is part of the saving. A nested order shows this: it goes from 282 bytes to 175 bytes, saving 107 bytes, and some of those are the zeroes dropped from 149.50 and 597.00.

Strings are left exactly as they are

Strings are the exception to every change above, because the characters inside a string are part of the value, not spacing. A message reading “hello world” keeps its space, and a note reading “keep these spaces” keeps its double and triple spaces, even though the same file drops from 64 bytes to 55 bytes around them. That is why minified JSON is safe to use: no label and no message can be damaged by the process, because the tool never looks inside a string at all.

Worked example: check the maths yourself

Take the tool’s own example, a on one line and b on the next, written across five lines with two-space indenting. Count it: the opening brace, a line break, two spaces, then “a”: 1, a comma, a line break, two spaces, then “b”: 2, a line break, and the closing brace: 22 bytes in total. Parse it, and you get an object with a set to 1 and b set to 2. Write it back with no spaces and you get {"a":1,"b":2}, which is 13 bytes. Nine bytes saved, or 40.9 percent, and the check takes less than a minute by hand.

The common mistake

People expect the order of the keys to change, because the tool has rewritten the file. It does not: the keys come out in the same order you wrote them in. A profile written name, age, city, subscribed comes back name, age, city, subscribed, only without the spacing. If the order in your output looks different, you are looking at a different file or a different tool: this one keeps the order as it was.

Frequently asked questions

What is the formula behind the JSON Minifier?

It parses the input into data with JSON.parse, then writes the data back with no spaces using JSON.stringify. Nothing inside a string value is changed.

Why did 149.50 become 149.5 in my minified JSON?

The tool reads numbers as values, then writes them in their shortest form. 149.50 and 149.5 are the same number, and 597.00 becomes 597 for the same reason.

Does minifying change the order of my keys?

No. The keys come out in the same order you wrote them in. Only the spacing is removed.

Are spaces inside strings removed too?

No. A note reading “keep these spaces” keeps its double and triple spaces, because spaces inside a string are part of the value.

More guides on the JSON Minifier

Try it yourself

Try the JSON Minifier — free, in your browser.

Open the JSON Minifier