How YAML is minified
YAML has two notations for the same data. Block style, the one everybody writes, uses indentation and line breaks: a key on one line, its nested keys indented below, list items starting with - . Flow style uses brackets like JSON: {name: deploy, on: {push: {branches: [main]}}}. Minifying converts every mapping and sequence to flow style and prints the whole document on one line with no wrapping. Comments are stripped, since they cannot survive the move to a single line, and blank lines disappear with them.
The conversion is done on the document model of the yaml library (eemeli), not with text substitution, so the output is guaranteed to parse back to the same values. Scalars keep the form you wrote: "nginx:1.27" stays quoted, version: 1.10 stays 1.10 rather than becoming the number 1.1, and port: "8080" keeps its quotes so it remains a string. Multi-line block scalars (| and >) cannot exist in flow style, so they become double-quoted strings with \n escapes holding the same text.
What survives
- Anchors and aliases:
&defaultsand*defaultsare written in place, and<<merge keys keep working. - Tags such as
!!binaryor custom!Reftags used by AWS CloudFormation stay attached to their values. - Dates and numbers keep their spelling, so
2026-09-14and1.10come out exactly as they went in. - Empty collections:
{}and[]stay empty collections rather than turning into null. - Key order, unless you switch on Sort keys in the toolbar, which orders every mapping alphabetically first. Sorted, minified output is a handy canonical form for comparing two configs.
YAML version still matters, because it controls how plain scalars are read. Under the default YAML 1.2, values like on, NO or yes are strings, and the minifier adds an info note that YAML 1.1 tools such as PyYAML and older Kubernetes libraries would read them as booleans; choose YAML 1.1 to get these as warnings. The formatting options Collections, String quotes and Indent lists under keys apply only to the beautified view.
Size, and when one line is the point
Flow style does not always save bytes. It replaces indentation and newlines with braces, commas and spaces after colons, so deeply nested, indented files shrink (the workflow in the first example goes from 248 to 184 bytes, mostly from the comments and indentation), while a flat file of short keys can come out the same size or a few bytes longer. The real reason to minify YAML is format, not size: a one-line value fits in a command-line flag such as helm --set-json or kubectl patch -p, an environment variable, a single cell of a spreadsheet, or a log line. If the consumer only needs data, the YAML to JSON converter produces strict JSON instead.
Minify one document at a time: a stream of several documents separated by --- is not yet written back cleanly as a single line. The block view is one Ctrl/Cmd+Shift+M away, and Ctrl/Cmd+Shift+C copies the line. Secrets inside Helm values or CI files are processed locally and never uploaded.
Examples
GitHub Actions workflow
Both comments are removed and the job steps become a flow list of mappings; an info note explains how YAML 1.1 tools read the on key.
# CI pipeline
name: deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install
run: npm ci # clean install
- name: Test
run: npm test
{name: deploy, on: {push: {branches: [main]}}, jobs: {build: {runs-on: ubuntu-latest, steps: [{uses: actions/checkout@v4}, {name: Install, run: npm ci}, {name: Test, run: npm test}]}}}Anchors, merge keys and a block scalar
The anchor, alias and merge key are written inline, and the literal block becomes the quoted string “Line one\nLine two\n”.
defaults: &defaults
adapter: postgres
pool: 5
development:
<<: *defaults
database: shop_dev
message: |
Line one
Line two
{defaults: &defaults {adapter: postgres, pool: 5}, development: {<<: *defaults, database: shop_dev}, message: "Line one\nLine two\n"}Compose services with sorted keys
With Sort keys on, db comes before web and image before environment, giving a stable one-line form for comparisons.
services:
web:
image: nginx:1.27
environment:
- TZ=Asia/Singapore
db:
image: postgres:16
{services: {db: {image: postgres:16}, web: {environment: [TZ=Asia/Singapore], image: nginx:1.27}}}Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Tabs are not allowed for indentation in YAMLExplained | A line is indented with a tab character, which the YAML specification forbids. | Replace the tab with spaces; most editors can convert indentation for the whole file. |
Duplicate key — keys in a mapping must be uniqueExplained | The same key appears twice at one level, often after merging two snippets. | Delete or rename one of them. The error points at the second occurrence. |
A nested block cannot be used as a keyExplained | A value contains an unquoted ": ", as in key: value: other, so the parser reads a second mapping where a value should be. | Quote the value, for example key: “value: other”. |
Missing closing "quote | A double-quoted string is never closed, so the rest of the file is read as part of it. | Add the closing double quote at the end of the value. |
Frequently asked questions
Is minified YAML still valid YAML?
Yes. Flow style is part of the YAML specification, and any compliant parser reads the one-line output as the same data as the original.
Why did my YAML get bigger after minifying?
Flow style adds braces, brackets and comma separators. For flat files with little indentation and no comments, those can outweigh the newlines removed.
Can I minify YAML to JSON?
Flow YAML looks like JSON but leaves most strings unquoted. Use the YAML to JSON converter when you need strict JSON.
Are comments kept when minifying YAML?
No. Comments cannot be represented on a single line without changing what they annotate, so they are removed. Keep the commented original in version control.