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.
{
"name": "webdevutility",
"tools": 56,
"tags": ["json", "csv", "base64"],
"offline": true
} {"name":"webdevutility","tools":56,"tags":["json","csv","base64"],"offline":true} 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.
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.
A settings object inlined into HTML is downloaded and parsed on every page load. A smaller object downloads faster and parses marginally faster.
A gzipped API response over HTTP. Gzip compresses runs of whitespace to almost nothing, so expect a saving of a few percent at most.
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.
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.
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 é.
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
$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.
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. 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.
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.
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.
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.
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.
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.
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.
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.