How to Minify JSON (and When to Beautify Instead)

How to Minify JSON (and When to Beautify Instead)

Try the JSON Formatter

How to Minify JSON (and When to Beautify Instead)

Minified JSON is JSON with every optional character removed — no indentation, no line breaks, no spaces between keys and values. It looks unreadable to humans, but that is exactly the point: smaller payloads travel faster across the network and cost less to store. In this guide you will learn how to minify JSON in the browser, in JavaScript, in Python, and on the command line, plus when you should not minify.

You can minify any JSON instantly with our JSON formatter — paste your data, switch to Minify mode, and copy the result. But it helps to understand what is actually happening under the hood.

What "minifying" JSON actually means

JSON has two kinds of characters: the ones that carry meaning and the ones that only make it readable. Minifying strips the second group.

Here is a formatted object:

{
  "id": 42,
  "name": "Ada Lovelace",
  "roles": [
    "admin",
    "editor"
  ],
  "active": true
}

And here is the exact same data, minified:

{"id":42,"name":"Ada Lovelace","roles":["admin","editor"],"active":true}

Both parse to an identical object. Not a single value changed — only the whitespace between tokens was removed. That is the key thing to remember: minifying never alters your data, only its presentation. The reverse operation, adding whitespace back, is called beautifying, prettifying, or formatting.

Why minify JSON?

Whitespace is invisible but not free. On a large document it can account for a surprising share of the file size.

  • Smaller network payloads. Every byte you send to a browser or mobile client costs bandwidth and latency. For an API returning thousands of records, dropping indentation can cut the response size by 10–30% before compression.
  • Faster page loads. JSON embedded in HTML (for example, server-rendered state or structured data) ships with the page. Smaller JSON means a smaller document and a quicker first paint.
  • Lower storage and transfer costs. If you store JSON in a database column, a cache, or object storage, minified records take less space and move faster between services.
  • Cleaner logs and message queues. Compact JSON keeps log lines and queue messages to a single line, which is easier to grep and cheaper to retain.

It is worth being precise about the savings. Gzip and Brotli — which almost every web server and CDN apply automatically — already compress repeated whitespace very efficiently. So the post-compression difference between minified and formatted JSON is often small. The wins are largest when compression is not in play: data embedded in code, stored uncompressed, or passed between in-memory systems.

How to minify JSON in the browser

The fastest way is to use a tool that runs locally so your data never leaves your machine:

  1. Open the JSON formatter.
  2. Paste or drag in your JSON.
  3. Click the Minify tab (or press Ctrl + Shift + M).
  4. Click Copy to grab the compact result.

Because everything runs client-side, you can safely minify sensitive payloads like API responses or config files without uploading them anywhere. If the tool reports an error instead of minifying, your JSON has a syntax problem — see common JSON errors to fix it, or run it through the JSON validator first.

How to minify JSON in JavaScript

The built-in JSON.stringify() minifies by default. When you omit the third "space" argument, the output has no extra whitespace:

const data = {
  id: 42,
  name: "Ada Lovelace",
  roles: ["admin", "editor"],
  active: true
};

// Minified — no third argument
const minified = JSON.stringify(data);
// '{"id":42,"name":"Ada Lovelace","roles":["admin","editor"],"active":true}'

// Beautified — pass an indent
const pretty = JSON.stringify(data, null, 2);

If you already have a formatted JSON string and want to minify it, parse and re-stringify it. This also validates the JSON as a side effect, since JSON.parse throws on invalid input:

function minify(jsonString) {
  return JSON.stringify(JSON.parse(jsonString));
}

How to minify JSON in Python

Python's json module uses the separators argument to control whitespace. The compact form removes the space after each comma and colon:

import json

data = {
    "id": 42,
    "name": "Ada Lovelace",
    "roles": ["admin", "editor"],
    "active": True,
}

# Minified: no spaces in separators
minified = json.dumps(data, separators=(",", ":"))
# '{"id":42,"name":"Ada Lovelace","roles":["admin","editor"],"active":true}'

# Beautified
pretty = json.dumps(data, indent=2)

The separators=(",", ":") part is what matters. By default json.dumps adds a space after commas and colons, so without it you get smaller than the default but not fully minified output.

How to minify JSON on the command line

If you have jq installed, the -c (compact) flag minifies any file or stream:

# Minify a file
jq -c . data.json > data.min.json

# Minify a curl response on the fly
curl -s https://api.example.com/users | jq -c .

Node.js works too, without extra dependencies:

node -e "process.stdout.write(JSON.stringify(JSON.parse(require('fs').readFileSync(0,'utf8'))))" < data.json

When to beautify instead

Minifying is the wrong choice in plenty of situations. Reach for formatting when:

  • You are debugging. A formatted, color-coded response is far easier to read than a single 4,000-character line. The tree view makes nested data even clearer.
  • The file is edited by hand. Config files like package.json, tsconfig.json, or CI manifests are read and changed by people. Keep them formatted so diffs stay readable.
  • It lives in version control. Minified JSON produces useless one-line diffs. Formatted JSON shows exactly which key changed in a pull request.
  • It is documentation or an example. Anything a human is meant to learn from should be readable.

A common workflow is to keep JSON formatted in your repository and minify it only at build or deploy time — so humans get readability and production gets the smaller file.

Does minifying lose data or precision?

No. Minifying only removes whitespace that sits between tokens, so every key, value, and structural character is preserved exactly. One subtle point: large numbers can lose precision when parsed by a language that uses 64-bit floats (JavaScript, for instance), but that is a property of parsing JSON at all — not of minifying. The text {"big":12345678901234567890} is identical whether formatted or minified; only your parser decides how to store it.

FAQ

Does minifying JSON make my API meaningfully faster?

It depends on whether compression is enabled. With gzip or Brotli — standard on most servers and CDNs — the size difference after compression is usually modest, because those algorithms already collapse repeated whitespace. The biggest wins come from minifying JSON that is stored or transmitted uncompressed, such as JSON embedded in HTML, cached as plain text, or passed between internal services.

Is minified JSON still valid JSON?

Yes. Whitespace between tokens is optional in the JSON specification, so removing it produces fully valid JSON. You can confirm any minified output with the JSON validator.

What is the difference between minify and compress (gzip)?

Minifying removes optional characters from the text itself, so the result is still readable JSON. Compression (gzip, Brotli) is a binary transformation applied on top — it shrinks the bytes further but the output is no longer JSON until it is decompressed. They stack: you can minify first and let the server compress the result.

Can I un-minify (beautify) JSON later?

Absolutely. Minifying is fully reversible because no data is lost. Paste minified JSON into the JSON formatter, choose Format, and pick 2-space, 4-space, or tab indentation to restore readability.

Should I commit minified JSON to Git?

Generally no. Minified files produce single-line diffs that hide what actually changed. Keep JSON formatted in version control and minify it as part of your build or deployment step instead.

How much smaller is minified JSON?

For typical, deeply nested data the raw text is often 10–30% smaller after minifying, though it varies with how much indentation the original used. After gzip the difference shrinks considerably. Test your own files in the JSON formatter — the status bar shows the byte size before and after.


Try it now: open the JSON Formatter, paste your data, and switch to Minify mode. To go the other way, see how JSON syntax works or convert your data with the JSON Converter.

Format JSON Instantly

Beautify, minify, and validate JSON with syntax highlighting and error detection.

Open JSON Formatter