What Does It Mean to Minify JSON?

JSON is often written with spaces, indents and line breaks so people can read it. Computers don’t need any of that. They read the data just as well when it is packed onto a single line.

Packing JSON down like this is called minifying. This guide explains what it removes and how much space it really saves. It also explains when you should keep your JSON easy to read.

Quick answer: To minify JSON means to remove every space, tab and line break that is not inside a string. The data stays exactly the same. The text just gets smaller and fits on one line.

What does minifying JSON remove?

Whitespace means characters you can’t see: spaces, tabs and line breaks. JSON allows whitespace between its parts. That means around brackets, colons, commas and values.

Whitespace is only there to help people read the file. Minifying removes all of it.

Before minifying:

{
  "id": 42,
  "name": "Blue Mug",
  "tags": ["kitchen", "gift"]
}

After minifying:

{"id":42,"name":"Blue Mug","tags":["kitchen","gift"]}

Notice that the space in "Blue Mug" is still there. It is inside a string, so it is part of the data. A minifier never touches the inside of strings.

Does minifying change the data?

No. Read the formatted version and the minified version with a JSON parser. You get the same value. Nothing is lost.

The JSON Minifier only removes whitespace outside of strings. It also checks your JSON first. If there is an error, it takes you to the error and does not minify.

There is one thing to know if you minify in code. Many programs minify by reading the JSON into memory. Then they write it out again. That step can change some values.

For example, JavaScript rounds whole numbers larger than 253. So a long ID can change without any warning. Our guide to big numbers in JSON explains this problem.

How much smaller does minified JSON get?

It depends on how much whitespace the file had. Each level of nesting adds more spaces at the start of every line inside it.

So deeply nested JSON with 4-space indents gets a lot smaller. A flat file with short lines gets only a little smaller.

The only way to know is to measure your own data. The JSON Size Analyzer shows the raw size, the minified size and the gzipped size side by side.

Gzip already compresses JSON. Do you still need to minify?

Gzip is a way to compress (pack) data. Most web servers use gzip, or something like it. They shrink responses before they send them. The browser unpacks them when they arrive, so you never see it happen.

Whitespace repeats a lot, and gzip is very good at shrinking repeated text. So with gzip on, the size difference between formatted and minified JSON is usually much smaller.

If your API already uses gzip, minifying often saves only a little. The Size Analyzer shows the gzipped size, so you can check this for your own data.

Minifying still helps in places where gzip doesn’t apply:

  • JSON stored in a database, a cache or browser storage.
  • JSON placed inside HTML, a cookie or a URL. Cookies and URLs have size limits.
  • Messages in a queue, or lines in a log file.
  • Prompts for AI models. A token is a small piece of text that an AI model reads. You often pay for each token. Spaces and line breaks use tokens too.

For that last case, the JSON Token Counter has a “Minify JSON first” option. Turn it on to see how many tokens your whitespace costs you.

When should you keep JSON easy to read?

Minified JSON is hard for people to work with. Keep it formatted when:

  • People edit the file by hand, like a config file.
  • The file is saved in Git. A minified file is one long line, so every small change shows up as “the whole line changed”.
  • The JSON is an example in documentation.
  • You are debugging and need to read the data.

A simple rule: minify for machines, format for people. You can always switch back. See how to format JSON to make a minified file readable again.

How to minify JSON in JavaScript

When you give JSON.stringify(value) only one argument, it adds no extra whitespace. So it already makes minified JSON:

const data = { id: 42, tags: ["kitchen", "gift"] };

const minified = JSON.stringify(data);
// {"id":42,"tags":["kitchen","gift"]}

const pretty = JSON.stringify(data, null, 2);
// the same data, indented with 2 spaces

If you start with JSON text, read it first: JSON.stringify(JSON.parse(text)). Remember the warning about big numbers above.

How to minify JSON in Python

Python’s json.dumps adds a space after every comma and colon by default. To remove them, set the separators option:

import json

data = {"id": 42, "tags": ["kitchen", "gift"]}

print(json.dumps(data))
# {"id": 42, "tags": ["kitchen", "gift"]}

print(json.dumps(data, separators=(",", ":")))
# {"id":42,"tags":["kitchen","gift"]}

By default, json.dumps also turns letters like “é” into escape codes, which makes the text longer. Add ensure_ascii=False to keep them as they are.

On the command line, the jq tool can do it too: jq -c . data.json prints the file in compact form.

Common questions

Is minified JSON still valid JSON?

Yes. Whitespace between values is optional in JSON, so a minified file is just as valid as a formatted one.

Can I un-minify JSON?

Yes. Any JSON formatter adds the line breaks and indents back. You choose the indent size. So it may not look exactly like the original, but the data is the same.

Does minifying JSON make an API faster?

It sends fewer bytes, which can help. But if the server already uses gzip, the gain is often small. Measure the gzipped size before and after to see if it is worth it.

Is minifying the same as compressing?

No. Minified JSON is still plain text, and any JSON parser can read it. Compression like gzip turns the text into binary data. You must unpack that data before you can read it. You can use both together.