YAML Formatter

Top-level keys—
Output lines—
Status—

How YAML Formatter is calculated: Parse the YAML with js-yaml (MIT, vendored on this site), then print it again with two-space indentation; a built-in subset parser covers mappings, lists, and scalars.

What is a YAML formatter?

A YAML formatter takes messy YAML and prints it again with clean, consistent indentation. Paste in a config file with uneven spacing, mixed indentation, or trailing spaces, and you get back the same data in a tidy two-space layout. The default text here has 4 top-level keys — name, age, city, and skills — and formats to 6 lines.

YAML is the config format of Docker Compose, Kubernetes, GitHub Actions, and Ansible, which means developers read it every day. A file with ragged indentation is hard to scan and easy to break: one wrong space can move a key under the wrong parent. Formatting is the fastest way to see the structure clearly.

This formatter uses js-yaml, a well-known open-source YAML parser, version 4.1.0, released under the MIT licence. The library file is vendored on this site at /vendor/js-yaml.min.js, so the page loads it from sortrun.com itself — no outside service is involved, and the conversion works offline once the page has loaded. A built-in subset parser runs only if the library is unavailable.

How YAML formatting works

Formatting has two steps: parse, then print. The parser reads your YAML and builds a JavaScript value — an object for a mapping, an array for a list, a string, number, boolean, or null for a plain value. The printer then writes that value back out as YAML, with two spaces per level of nesting, which is the standard style.

Parsing is the part that catches errors. If a line has no colon where a key is expected, or a list item is indented wrongly, the parser reports the line number instead of guessing. The default text parses cleanly: name, age, and city are scalars, and skills is a list of two items, python and rust.

Printing is the part that fixes the style. A mapping key is printed as "key: value", a nested mapping is indented two more spaces, and a list item is printed as "- value" under its key. Strings that look like numbers or booleans are quoted, so "yes" does not become a boolean by accident. The result has 6 lines for the default input, and every level of nesting is exactly two spaces deeper than its parent.

Indentation: the rule people get wrong

YAML cares about indentation more than almost any other format. Two spaces per level is the convention, and mixing two-space and four-space indentation in one file is the most common error. The formatter removes that problem entirely: whatever you paste in, the output uses two spaces everywhere.

Tabs are not allowed in YAML at all — the specification forbids them for indentation. A file copied from an editor that uses tabs will fail to parse, and the error message names the line. Replace tabs with spaces before pasting, or let your editor convert them, and the formatter will handle the rest.

Lists under keys are the second trap. In the default text, "skills:" is followed by " - python" — the dash is indented two spaces under the key. Some writers put the dash at the same level as the key, which is also valid YAML, but the formatter normalises it to the indented style so every file on your team looks the same.

The rule that catches most people is that indentation must be consistent within a level, but the size of the step is free. Two spaces, four spaces, or any other width all work, as long as every key at the same depth uses the same width. The formatter picks two spaces, which is the most common choice in the wild, and rewrites every level to that width. A file that mixes two-space and four-space indentation will parse, but it is hard to read, and the formatter's job is to make the structure visible at a glance rather than to change what the file means.

A worked example: tidying a config

Take the default text: "name: Ada", "age: 36", "city: London", and a skills list with python and rust. It has 4 top-level keys. Paste it in and the formatter returns the same data in 6 lines, with the list items indented two spaces under "skills:". Nothing about the data changes — only the spacing.

Try a messier version: write the same keys with four-space indentation and an extra blank line, and the output is identical to the tidy version. That is the point of a formatter — the input can be sloppy, and the output is always the same clean shape, which makes diffs in version control readable.

Try an error to see the other side: "name: Ada" followed by "this line has no colon" fails to parse, and the output reports the problem instead of producing bad YAML. The status changes from Valid to Invalid, and the line count drops to 0. A YAML to JSON converter is the next step when you need the same data as JSON instead.

Who uses a YAML formatter, and why

Developers working with Docker Compose, Kubernetes, or CI pipelines are the main users. A compose file with twenty services is unreadable when the indentation drifts, and a formatter brings it back to a shape the whole team can scan. It is also the fastest way to check whether a file that fails to load is broken by structure or by spacing.

DevOps engineers and platform teams are the second group. Helm charts, Ansible playbooks, and GitHub Actions workflows are all YAML, and a formatter is part of the daily routine of editing them. Formatting before committing keeps pull requests clean, because the diff shows only the real change, not a re-indentation.

Beginners are a third group. YAML looks simple until the first indentation error, and a formatter shows what correct YAML looks like. A JSON formatter does the same job for JSON, and the two tools together cover the formats most developers touch every day.

DevOps engineers are a third group, and they format YAML more often than anyone. A Kubernetes manifest, a CI pipeline, and an Ansible playbook are all YAML, and a manifest that was edited by three people in a hurry will have ragged indentation that hides a wrong nesting level. Running it through the formatter before committing makes the structure visible, and a wrong nesting level — a key that belongs to one block but sits under another — is easy to spot once the indentation is clean.

YAML formatter vs other converters

A formatter changes style, not format. The YAML to JSON converter on this site changes the format itself, turning the same data into JSON, while this tool keeps it as YAML and only tidies the layout. Use the formatter when the file stays YAML, and the converter when the consumer needs JSON.

The other direction matters too. A JSON to YAML converter turns JSON into YAML, which is what you need when a tool gives you JSON and your pipeline wants YAML. The formatter sits in the middle of that workflow: convert to YAML, then format it so it reads cleanly.

All three tools share the same parser, so they agree on what valid YAML is. A file that formats here will convert cleanly there, and a file that fails here will fail there too, with the same line number. That consistency is the point of sharing one library across the tools.

Common YAML mistakes

The first mistake is tabs for indentation. YAML forbids tabs, and a file with tabs fails with an error that names the line. Set your editor to insert spaces, and the problem never appears.

Second: inconsistent indentation. Mixing two-space and four-space levels in one file confuses the parser and confuses readers. The formatter fixes the output, but the file you paste in should be written with one width throughout — two spaces is the standard.

Third: unquoted strings that look like other types. "yes", "no", "on", and "off" are strings in YAML 1.2, but older parsers read them as booleans, and "2024-01-01" can be read as a date. When a value must stay a string, quote it: "yes" in quotes is always a string.

Fourth: forgetting that a colon needs a space. "key:value" is a plain string, not a mapping, because YAML requires a space after the colon. "key: value" is the mapping. If a key seems to vanish, check for the missing space first — it is the most common single-character error in YAML.

A second mistake is using tabs for indentation. YAML forbids tabs, and a file with a tab in the indentation will fail to parse even though it looks right in an editor that displays tabs as spaces. The error message usually points at the line with the tab, but the message is cryptic, and the first fix to try is always the same: replace every tab with spaces. The formatter does that automatically when it re-emits the file, which is why pasting a tab-indented file through the formatter is the fastest way to fix it.

How to use this YAML formatter

Paste your YAML into the box, or type it directly. The formatted YAML appears instantly in the box below, with two-space indentation throughout. The counters above it show 4 top-level keys and 6 output lines for the default text, and the status reads Valid when the input parses cleanly.

To copy the result, use the Copy button next to the output box. To check a different file, clear the box and paste again. If the input has an error, the output reports the line number instead of producing bad YAML — fix that line and paste again.

Everything runs in your browser. No sign-up, no account, no data sent anywhere — your YAML never leaves your device. It works offline once the page has loaded, because the js-yaml library is vendored on this site rather than fetched from a CDN.

Paste the YAML, press format, and read the result before you copy it. The result is the same data with clean two-space indentation, and any error in the input is reported with the line number so you can fix it at the source. If the input is a fragment rather than a full document, wrap it in a mapping first — a bare list at the top level formats fine, but a bare scalar such as a single word is not a document the formatter can improve. When the output looks right, copy it and paste it back into the config file it came from.

Frequently Asked Questions

Two spaces per level, which is the standard YAML style. Whatever indentation you paste in — two spaces, four spaces, or a mix — the output uses two spaces everywhere, so every file looks the same.

No. Formatting parses the YAML into data and prints it again, so the keys, values, and structure are identical — only the spacing changes. The default text has 4 top-level keys before and after formatting.

The output reports the problem instead of producing bad YAML, usually with the line number. The status changes from Valid to Invalid and the line count drops to 0. Fix the named line and paste again.

Yes. The js-yaml library (version 4.1.0, MIT licence) is vendored on this site at /vendor/js-yaml.min.js, so the page loads it from sortrun.com itself. A built-in subset parser covers mappings, lists, and scalars if the library is unavailable.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.