YAML Formatter
Format and validate YAML. Reports syntax errors with line and column. Preserves comments and anchors.
Format, indent, and validate YAML. Detects syntax errors with line numbers. Preserves comments, anchors, and multi-document streams. Read more Show less
What is YAML?
YAML ("YAML Ain't Markup Language") is a human-readable data serialization format. It is used for configuration files, CI pipelines, Kubernetes manifests, Ansible playbooks, Docker Compose files, OpenAPI specs, and anywhere else a format needs to be written and read by humans more often than by machines.
YAML is a superset of JSON: any valid JSON document is also valid YAML. But YAML is far more permissive - it allows indentation instead of braces, omits most quotes, supports comments, anchors, aliases, and multi-line strings, and can express the same data in many different ways. That flexibility is what makes it pleasant to write and also what makes it easy to write incorrectly.
How to use
Paste YAML into the input. If it parses, the output shows the same document reformatted with consistent indentation. If it does not parse, the error panel shows the line and column of the first problem.
Two formatting options:
- Indent - 2, 4, or 8 spaces. Block sequences in YAML conventionally use 2 spaces, but some teams prefer 4.
- Line width - controls whether long strings and flow collections get folded across multiple lines. Default is no folding (line width 0), which keeps everything on one line per value.
YAML vs JSON
JSON is a subset of YAML with stricter rules and no comments. If your data has no comments and you do not need YAML-specific features like anchors or multi-line strings, JSON is usually the better choice - it is unambiguous, universally supported, and every YAML parser handles it.
YAML wins when humans write the file by hand. Comments, anchors, and the freedom to omit quotes make configuration files substantially more maintainable. YAML loses on ambiguity: the same logical value can be written six different ways, and some of those ways have surprising interpretations.
Common pitfalls
Tabs are forbidden. YAML uses spaces for indentation only. A tab anywhere at the start of a line is a parse error. This is the single most common cause of "invalid YAML" errors, usually caused by pasting from a code editor configured for tabs.
The Norway problem. Unquoted no, yes, on, off, true, and false are parsed as booleans. So country: NO becomes { country: false }, not { country: "NO" }. Always quote country codes and any other value that might be a YAML keyword.
Colon and space. A colon in a mapping key must be followed by a space. key:value is one scalar string; key: value is a mapping. Missing space after a colon is the second most common YAML bug.
Version numbers. version: 1.0 parses as the number 1, then the string ".0", then YAML gets confused. Always quote version strings: version: "1.0".
Comments eat indentation. A comment line's indentation matters for the block it belongs to. Misaligned comments cause errors that point to the wrong line.
Anchors and aliases
YAML supports anchors and aliases to avoid duplication. An anchor is defined with &name and referenced with *name:
defaults: &base timeout: 30server1: <<: *base
This sets server1.timeout to 30 by merging the anchored map. Anchors and aliases are preserved by the formatter; they are expanded only if you convert to JSON.
FAQ
Why did my YAML change meaning after formatting?
It should not. A formatter only changes whitespace; the parsed data structure is identical. If the reformatted output looks different in meaning, the input was misparsed to begin with - typically a missing space after a colon, or a value that was interpreted as a boolean or number when you meant a string.
Can I format multiple documents in one file?
Yes. Multi-document YAML uses --- as a separator and is common for Kubernetes manifests. This tool parses and formats each document in the stream independently.
Does formatting preserve comments?
Yes. The parser preserves comments as part of the document structure, and the formatter writes them back in their original positions, adjusting indentation to match the new layout.
Why is line width 0 the default?
Folding is a YAML feature for wrapping long lines at a specific column. It is useful for humans reading files in narrow terminals, but it changes the shape of the output and can make diffs harder to read. Most teams disable folding and let their editor wrap the display instead.
What is the maximum nesting depth?
YAML itself has no limit, but this tool uses the standard JavaScript yaml library, which will fail on deeply recursive structures. For normal configuration files you will never hit the limit.
Can I convert YAML to JSON with this tool?
Use the dedicated YAML to JSON tool for that. This tool only formats YAML; it does not change the format.
Command line equivalent
# Python
python3 -c 'import yaml,sys; print(yaml.safe_dump(yaml.safe_load(sys.stdin), indent=2, default_flow_style=False))' < input.yml
# Ruby
ruby -ryaml -e 'puts YAML.load_file("input.yml").to_yaml'
# Node
npm i yaml
node -e 'const YAML=require("yaml"), fs=require("fs"); const d=YAML.parseDocument(fs.readFileSync("input.yml","utf8")); process.stdout.write(d.toString({indent:2}))'
# yq (Go version)
yq -P input.yml
# yq (Python version)
pip install yq
yq -y . input.yml