Skip to content

Minifying JSON and why YAML is never just minified

Blog / Published 

Minifying a JSON file means removing every space, tab and line break that is not inside a string. The data stays the same, and the file gets smaller. That matters for API responses and config files that are sent over a network on every request, where a few kilobytes repeated thousands of times add up.

What minification changes and what it keeps

Whitespace between tokens has no meaning in JSON, so a parser reads {"a": 1} and { "a" : 1 } as the same value. Whitespace inside a string does matter. The text "hello world" keeps its three spaces, because they are part of the value. A correct minifier skips strings and touches only the spaces outside them.

A minifier that reorders keys, changes number formats or drops values is doing more than minifying, so check its output before you use it.

Why YAML is different

YAML uses indentation to show structure, so the indentation is part of the meaning. Removing it would change which key belongs to which parent. Instead, YAML is usually converted to a flow style, where the structure is written with braces and brackets, and that flow form is then as compact as JSON. YAML 1.2 is a superset of JSON, so a valid JSON document is also valid YAML, but the reverse is not true.

When a file needs to be smaller, convert it to JSON with the YAML to JSON converter and minify the result. If the file must stay YAML, keep the indentation and remove only comments you no longer need.

Formatting for reading

Minified JSON is hard to read, so the reverse step is just as common. The JSON formatter adds indentation back and shows where each object begins and ends. The YAML formatter does the same for YAML while keeping the structure intact.

Checks before you deploy a minified file

Written by Ján Turský

Creator of TextArray — building free, privacy-first tools that run entirely in your browser.