How to Minify YAML (and Why You Usually Convert Instead)
You can't squash YAML onto one line the way you minify JSON — its indentation is the structure. Here's what minifying YAML really means, and when JSON is the better answer.
Why YAML can't collapse like JSON
Minifying JSON is simple: strip all the insignificant whitespace and you get one compact line that parses identically. YAML doesn't work that way. Its indentation and line breaks are the structure — remove them and you either change the data or produce something that no longer parses. So "minify YAML" can't mean "remove all whitespace." It means something narrower.
What a YAML minifier can safely remove
A safe minifier strips only the parts of a YAML file that carry no data:
- Comments — every
#line and trailing comment. - Blank lines between entries.
- Trailing whitespace at the end of lines.
- Optional markers like a redundant
---start or...end on a single-document file. - Excess indentation — normalizing 4-space indent down to 2, the smallest consistent size.
What it cannot remove is the structural indentation itself, or the line breaks that separate keys and list items. Those are load-bearing.
Flow style: YAML's compact form
YAML does have a compact representation — flow style — which uses JSON-like braces and brackets instead of indentation:
# block style
server:
host: localhost
port: 8080
roles:
- admin
- editor
# flow style
{server: {host: localhost, port: 8080}, roles: [admin, editor]}
Flow style is valid YAML and fits on one line, so it's the closest thing to true minification. But notice the irony: flow-style YAML looks almost exactly like JSON — because JSON essentially is YAML's flow style. Which points to the real answer for most use cases.
If you want it small, convert to JSON
When the goal is the smallest, fastest payload for a machine to consume, the right move usually isn't to minify YAML — it's to convert it to minified JSON. JSON is more compact, parses faster in every language, and YAML's data model maps onto it cleanly. Keep YAML as the human-edited source of truth, and emit minified JSON at build or request time.
As with any format, transport compression matters more than minification. Over gzip or Brotli, a commented, nicely-indented YAML file and its stripped version are within a couple of percent of each other on the wire. Minify or convert when bytes are stored uncompressed or parsing speed matters — not reflexively.
Minify your YAML now
Strip comments, blank lines, and excess whitespace while keeping the file valid YAML — or convert it to compact JSON. Runs in your browser.
Open YAML Minifier →Frequently Asked Questions
Can you minify YAML to a single line like JSON?
Not in block style — YAML's indentation is structural, so you can't remove line breaks the way you can with JSON. You can rewrite it in flow style (JSON-like braces and brackets) or strip comments and whitespace. The most compact result is usually just valid JSON, since YAML is a superset of it.
What does a YAML minifier actually remove?
It removes things that carry no data: comments, blank lines, trailing whitespace, and redundant document markers. It can also normalize indentation to the smallest consistent size. It cannot remove the structural indentation itself without changing or breaking the document.
Should I minify YAML or convert it to JSON?
If the goal is the smallest payload for machines, convert to JSON — it's more compact, parses faster, and YAML's data model maps onto it cleanly. Keep YAML when humans need to read or edit the file. Minifying YAML in place mainly strips comments and whitespace while keeping it valid YAML.
Is flow-style YAML the same as JSON?
Very nearly. JSON is essentially YAML's flow style, which is why YAML 1.2 is a superset of JSON. Flow-style YAML allows a few things JSON doesn't (unquoted strings, comments outside the braces), but for plain data the two forms are interchangeable.