Home / Guides / JSON minification guide

JSON Minification Explained

What minifying JSON means, what is safe to remove, how it relates to gzip, how to do it in code, and what can go wrong.

Last updated

Minify JSON now with the JSON minifier.

What JSON minification is

Minifying JSON means removing every character that a parser does not need, without changing the data. In practice that is the whitespace between tokens: spaces, tabs and line breaks. The result is the same JSON in fewer bytes.

{
  "name": "Sam",
  "tags": ["a", "b"],
  "active": true
}

becomes

{"name":"Sam","tags":["a","b"],"active":true}

What can be removed

  • Whitespace outside strings: always safe.
  • Comments and trailing commas: not part of JSON at all. Some config formats (JSONC, JSON5) allow them, and a minifier for those removes them.
  • Redundant escapes: \u0041 can be written as A, and \/ as /.
  • Number text: 1.50 can be 1.5 and 1E+05 can be 1e5. The value is the same.

Whitespace inside a string is data and must stay. A newline in a string is written as \n, not as a real line break.

Minify, compress and gzip: three different things

  • Minify: fewer characters, still plain JSON.
  • Compress the data: removing keys with null or empty values, or rounding numbers. This changes the data, so it is a decision about your format, not a mechanical step. See the JSON compressor.
  • gzip or Brotli: a transport encoding that the server applies and the browser undoes. It works on repeated text, so it also removes most of the cost of whitespace.

Pretty-printed JSON compresses well, so the gain from minifying on top of gzip is often only a few percent. Minifying still matters when there is no HTTP compression: data in a cookie, a URL parameter, local storage, a message queue, an embedded script tag or a mobile app bundle.

How much does it save?

Indented JSON typically shrinks by 10 to 40 percent. Deep nesting and 4-space indentation give the largest savings. Flat data with long strings saves little.

How to minify JSON in code

// JavaScript
const min = JSON.stringify(JSON.parse(text));

# Python
import json
min = json.dumps(json.loads(text), separators=(",", ":"), ensure_ascii=False)

// PHP
$min = json_encode(json_decode($text), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

# Command line
jq -c . data.json

Does minifying JSON help page speed and SEO?

Search engines do not rank a page higher because its JSON is minified. The benefit is indirect: smaller HTML and faster API calls improve page speed, which feeds into user experience signals such as Core Web Vitals. JSON-LD structured data is read the same way whether it is formatted or minified, so you can minify it freely.

Minify JSON in more languages

// Go
var buf bytes.Buffer
json.Compact(&buf, data)

# Ruby
JSON.generate(JSON.parse(text))

// C# (System.Text.Json writes compact JSON by default)
JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(text))

Build pipelines

Most bundlers already write imported JSON files compactly in a production build, so a separate step is usually not needed. For files you serve as they are, such as API fixtures or a public data file, add a minify step to your deploy script and keep the formatted file in source control.

Pitfalls

  • Big numbers. JSON.parse in JavaScript and json_decode in PHP turn integers above 9007199254740991 into floating point numbers and lose digits. If you minify through code, IDs may change. The minifier on this site keeps number text exactly as written.
  • Duplicate keys. Parsers keep one value and drop the others, so a parse and stringify round trip can lose data.
  • Key order. Most round trips keep the order, but sorting keys changes it. Only sort when order does not matter.
  • Escaping non-ASCII. Python escapes é as \u00e9 by default (ensure_ascii), which makes the file larger. Turn it off if your system supports UTF-8.
  • Byte order mark. A file that starts with a BOM is not valid JSON for some parsers. Remove it.
  • JSON Lines. A file with one JSON document per line is not one JSON value. Minify each line on its own.

When not to minify

Keep files that people read and edit, such as config in a repository, in a formatted layout. Git diffs work line by line, and a one-line file makes every change a whole-file change. Minify at build time or at the edge, and keep the readable source. Check the result with the JSON validator, and use the formatter or beautifier to read minified data again.

Frequently asked questions

What does it mean to minify JSON?

It means removing the whitespace that a parser does not need, so the same data takes fewer bytes.

Is minified JSON the same as compressed JSON?

No. Minified JSON is still plain text. Compressed data, such as gzip, is binary and must be unpacked first.

Should I minify JSON if my server uses gzip?

It still helps a little, and it helps a lot where gzip is not used, such as cookies, local storage and embedded data. The gain over gzip alone is small.

Can minifying break JSON?

Not by itself. Problems come from the way you minify: a parse and stringify round trip can change big numbers and drop duplicate keys.

Should I minify JSON-LD structured data?

You can. Search engines read minified JSON-LD the same way. Keep a formatted copy in your source and minify when you generate the page.

How do I minify JSON in Python?

Use json.dumps(json.loads(text), separators=(",", ":"), ensure_ascii=False). The separators remove the spaces after the comma and colon.