"category": "inspect-and-test",

YAML Formatter

"tldr": Paste messy YAML and get it re-formatted with consistent 2-space indentation and normalized style - meaning unchanged.

Re-format any YAML document into clean, consistent block style: 2-space indentation, normalized quoting, flow-style mappings ({a: 1}) expanded into readable blocks. The document is parsed and re-emitted, so the output is guaranteed valid and semantically identical.

{"yaml": "formatted yaml"}

Consistent formatting matters more in YAML than almost any other format because indentation is the syntax - a config file assembled by three people with three editors is a diff minefield. Normalizing before commit keeps reviews about the values, not the whitespace.

How to format YAML

  1. 1Paste your YAML - inconsistent indentation and flow style are both fine as input.
  2. 2The document is parsed (errors reported with line numbers) and re-emitted in block style.
  3. 3Indentation is normalized to 2 spaces; quotes are added only where YAML requires them.
  4. 4Copy the result - it is semantically identical to your input.

Convert YAML to Formatted YAML in code

JavaScript (js-yaml)
import yaml from "js-yaml";

const formatted = yaml.dump(yaml.load(messy), { indent: 2 });
Command line (yq)
# re-emit normalized YAML in place
yq -i -P '.' config.yaml

Frequently asked questions

Will formatting change what my config means?

No - the document is parsed to data and re-serialized, so the meaning is preserved exactly. What does change: comments are dropped (YAML comments are not part of the data model), key styling is normalized, and anchors/aliases are expanded. Keep a copy if comments matter.

Why did my comments disappear?

Parse-and-re-emit formatters work on the data, and comments are not data. For comment-preserving formatting, use a style-aware tool like Prettier (which formats YAML without full re-serialization) or yamlfmt.

Flow style vs block style - which should I use?

Block style (indented, one key per line) for anything humans edit - it diffs cleanly and reads well. Flow style ({a: 1, b: 2}) is compact and fine for short inline structures. This formatter expands to block style because config files are its main use case.

Can I format YAML in my editor or CI instead?

Yes - Prettier handles YAML in VS Code (comment-preserving), and yamlfmt or yq can normalize in CI. This tool is for the quick one-off: paste, normalize, paste back.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →