Xerobit

JSON Minifier: Remove Whitespace and Compact JSON

Paste your JSON into the input field to instantly remove all unnecessary whitespace, newlines and indentation. This json minifier produces a compact string suitable for network requests or storage, and it runs entirely in your browser so nothing is uploaded.

Minified JSON

{"name":"example","values":[1,2,3]}

How to use the json minifier

  1. Paste your formatted or pretty-printed JSON into the text area.
  2. The json minifier automatically parses and compacts the data as you type.
  3. Copy the minified output from the result box for use in your code or API calls.

How JSON minification works

A json minifier reduces the size of a JSON document by stripping out every character that is not required for parsing. The process relies on JSON.parse to read the input into a native data structure, then JSON.stringify without any spacing arguments to serialize it back into a single line.

For example, consider this formatted snippet:

{
  "user": "admin",
  "active": true
}

The parser reads the keys and values, discarding the two spaces of indentation and the newline characters. The serializer outputs {"user":"admin","active":true}, which is 28 characters instead of 40. That is a 30 percent reduction in payload size.

What gets removed and what stays

Not all whitespace is treated equally. The JSON specification defines four structural whitespace characters: space (U+0020), horizontal tab (U+0009), line feed (U+000A) and carriage return (U+000D). When these appear outside of string literals, they are purely cosmetic and safe to remove.

Whitespace inside string values must be preserved. If your JSON contains {"msg": "hello world"}, the three spaces between the words are part of the data. A correct minify json operation will never alter them. Similarly, escape sequences like \n or \t within strings remain intact because they represent literal characters in the parsed value.

When to use compact JSON

Compact JSON is most useful when data travels over a network. REST APIs, WebSocket messages and configuration files loaded at startup all benefit from smaller payloads. Removing whitespace from a 500 KB response might save 50 to 100 KB, which adds up across thousands of requests.

Another common scenario is storing JSON in databases or key-value stores where each byte costs money. Tools like json formatter do the opposite: they add indentation for human readability during debugging, while this tool strips it for production use.

If you need to compare two JSON documents structurally, minifying both first ensures that formatting differences do not cause false mismatches. You can then pass the results to a json diff tool for accurate comparison.

Common mistakes with manual minification

Developers sometimes try to minify JSON using regular expressions or simple string replacement, such as removing all spaces globally. This approach breaks valid JSON whenever a string value contains spaces. For instance, replacing every space in {"key": "my value"} yields {"key":"myvalue"}, silently corrupting the data.

Another mistake is forgetting trailing commas. Standard JSON does not allow trailing commas, but some JavaScript environments tolerate them. A strict parser will reject [1, 2,] before minification even begins. Always ensure your input is valid JSON first. If you are working with other formats, convert them using tools like csv to json or yaml to json before minifying.

Size comparison reference

The table below shows typical savings when minifying different kinds of JSON structures. Actual results depend on how much indentation and how many newlines the original document contains.

Structure typePretty sizeMinified sizeReduction
Flat object (5 keys)~120 bytes~80 bytes~33%
Nested object (3 levels)~300 bytes~180 bytes~40%
Array of 100 numbers~600 bytes~400 bytes~33%
Array of objects~2000 bytes~1100 bytes~45%

Deeper nesting and longer arrays tend to yield higher percentage reductions because each level of indentation adds more whitespace characters per element.

Worked examples

Each result below was produced by the same code the tool runs, so you can check your own numbers against them.

ExampleInputResult
Simple objectJSON input: { / "name": "Alice", / "age": 30 / }{"name":"Alice","age":30}
Nested arrayJSON input: [ / 1, / 2, / [ / 3, / 4 / ] / ][1,2,[3,4]]
String with spacesJSON input: {"message": "hello world"}{"message":"hello world"}

FAQ

Does minifying JSON change the data?

No. Minification only removes structural whitespace like indentation and newlines. All keys, values, string contents and numeric precision remain exactly the same.

Is this json minifier safe for sensitive data?

Yes. The processing happens entirely in your browser using native JavaScript functions. Your JSON is never sent to any server.

Can I minify JSON that contains unicode characters?

Yes. Unicode characters inside strings are preserved correctly. The parser handles all valid UTF-8 encoded JSON input.

Why would I minify instead of using gzip compression?

Minification and gzip serve different purposes. Minification reduces the raw size before encoding, which also lowers memory usage during parsing. Using both together gives the smallest possible transfer size.

What happens if my JSON is invalid?

The tool will display an error message indicating where the parser failed. You need to fix syntax issues like missing quotes, trailing commas or unmatched brackets before minification can succeed.

Does this handle very large JSON files?

It handles files that fit comfortably in your browser's memory. For extremely large datasets measured in hundreds of megabytes, a command-line tool or streaming parser may be more appropriate.