JSON Minifier

Strip every space and line break out of JSON and get one compact line back. Use it when the JSON is going into a database column, an environment variable or a data attribute, where no gzip step runs over it.

JSON Input
Minified Output

1 JSON Minifier example

Formatted (127 bytes)

1
2
3
4
5
6
{
  "name": "webdevutility",
  "tools": 56,
  "tags": ["json", "csv", "base64"],
  "offline": true
}

Minified (86 bytes)

1
{"name":"webdevutility","tools":56,"tags":["json","csv","base64"],"offline":true}

2 Where the saving matters

Stored, not served

A JSON blob in a database column, a localStorage entry or a data attribute is stored byte for byte. No gzip runs over any of those, so you pay for every indentation space.

Message payloads

Queues and webhooks often bill by payload size or cap it. Removing the indentation from a large event body costs nothing and leaves more room under the limit.

Config shipped to the client

A settings object inlined into HTML is downloaded and parsed on every page load. A smaller object downloads faster and parses marginally faster.

When it barely matters

A gzipped API response over HTTP. Gzip compresses runs of whitespace to almost nothing, so expect a saving of a few percent at most.

3 What survives minifying

Key order

Keys come out in the order they went in, which keeps diffs of minified files readable. The exception is integer-like keys such as "2" and "10": JavaScript objects list those first, in ascending order.

Numbers

Values keep their meaning but not always their spelling. The JSON is parsed and written out again, so 1.0 comes back as 1 and 1e3 as 1000. Integers above 2^53 lose precision, so send 64-bit IDs as strings.

Unicode

Accents and emoji stay as literal UTF-8 characters. Unicode escapes in the input are written out as the plain character, so a 6-character escape for é becomes the 2-byte é.

How to use the JSON Minifier tool

  1. Paste formatted JSON into the left panel, or use Upload File to load one from disk.
  2. The minified version appears on the right as you type, with the size before and after, and the percentage saved, in the panel header.
  3. If the JSON is invalid, the parser's error shows in red instead. Fix it and the output comes back.
  4. Copy Result puts the single line on the clipboard, and Download saves it as minified.json.

How to minify JSON in JavaScript, PHP and Python

JavaScript / Node.js

const formatted = `{
  "name": "webdevutility",
  "tags": ["json", "csv"]
}`;

// Parse and re-serialise without spacing
const minified = JSON.stringify(JSON.parse(formatted));
console.log(minified);
// {"name":"webdevutility","tags":["json","csv"]}

// The reverse, for reading it again
console.log(JSON.stringify(JSON.parse(minified), null, 2));

// A whole file
const fs = require("node:fs");
fs.writeFileSync("out.json", JSON.stringify(JSON.parse(fs.readFileSync("in.json", "utf8"))));

JSON.stringify with no second and third argument already produces the minified form, so there is no separate minifier to install.

PHP

<?php
$formatted = file_get_contents('in.json');

// Decode then re-encode. json_encode has no whitespace by default.
$data = json_decode($formatted, true, 512, JSON_THROW_ON_ERROR);
$minified = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

file_put_contents('out.json', $minified);

// Without those two flags PHP escapes / as \/ and every accent as \uXXXX,
// which makes the output larger, not smaller.

On content that is not pure ASCII, JSON_UNESCAPED_UNICODE often saves more than removing the whitespace does.

Python

import json

with open("in.json") as f:
    data = json.load(f)

# separators matters: the default leaves a space after each comma and colon
minified = json.dumps(data, separators=(",", ":"), ensure_ascii=False)

with open("out.json", "w") as f:
    f.write(minified)

# ensure_ascii=False keeps é as é instead of \u00e9, which is
# both smaller and readable.

Common errors and how to fix them

SyntaxError: Unexpected token } in JSON at position 42
A trailing comma before a closing brace or bracket: valid JavaScript, invalid JSON. The position in the message points at the character after the problem.
Unexpected token ' in JSON
Single quotes. JSON requires double quotes on both keys and string values.
The minified file is barely smaller
The content was mostly data rather than structure, or it was already compact. Whitespace is typically 15 to 30% of formatted JSON, and a file that is already one line has none to remove.
PHP output is full of \/ and \u00e9
json_encode escapes slashes and non-ASCII by default. Pass JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE.
Large numbers changed value
JSON numbers are parsed as 64-bit floats, so integers past 2^53 lose precision. Send IDs of that size as strings, in both directions.

Good to know

Minify JSON where nothing else compresses it. Over HTTP with gzip or brotli, whitespace compresses to almost nothing. Minifying helps for a JSON column in a database, a value in localStorage, a data attribute in HTML, a message in a queue that bills by payload size, and anything that must fit a fixed limit such as a cookie or an environment variable.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

How much smaller does JSON get?

Formatted JSON is typically 15 to 30% whitespace, and more when it is deeply nested and indented with 4 spaces. The panel header shows the size before and after, and the percentage saved.

Does minifying change the data?

Not the data. It parses the JSON and writes it out again without spacing, so values, types and key order survive. However, a few spellings change: 1.0 becomes 1, Unicode escapes become the plain character, and integer-like keys move to the front.

Should I minify JSON if the server already gzips?

Gzip compresses repeated whitespace very well, so the extra saving on the wire is small. Minify anyway for anything stored uncompressed: a database column, localStorage, a data attribute in HTML.

Why do I get a syntax error?

The input has to be valid JSON, which is stricter than a JavaScript object: double-quoted keys, no trailing commas, no comments. The error message gives the position of the first character the parser could not accept.

Can I undo it?

Yes, with the JSON formatter. Minifying drops only the layout and a few number spellings, so formatting the result gives you the same data back.

Going the other way

To read a minified blob again, paste it into the JSON formatter. Minifying changes the layout and a few number spellings, so apart from integers above 2^53 the formatted result carries the same data you started with. For stylesheets and scripts there are the CSS minifier and JS minifier.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!