Code Formatting

YAML Formatter

Format and validate YAML, and convert it to JSON.

YAML input
Result

What is YAML Formatter?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files.

Features

  • Format: Beautify YAML with consistent indentation
  • Validate: Check YAML syntax
  • Convert to JSON: YAML to JSON conversion
  • Custom Indent: 1-8 spaces

Use Cases

  • Docker Compose files
  • Kubernetes configurations
  • CI/CD pipelines (GitHub Actions, GitLab CI)

Paste a YAML file and the tool parses it with js-yaml 4, then re-serialises the result, reports the first syntax error with its line and column, or converts the document to JSON. Useful on Docker Compose and Kubernetes manifests, where a stray tab or an unquoted 1.20 breaks a deploy.

Formatting is a parse and dump, not a text rewrite

The formatter never touches your characters. It runs the input through js-yaml 4.1.1 into plain JavaScript values, then writes those values back out. Types are therefore resolved on the way in: under the YAML 1.2 core schema only true and false (plus capitalised forms) are booleans, so yes, no, on and off stay strings, unlike parsers still using the 1.1 rules. A bare 1.20 becomes the number 1.2, so a version string needs its quotes. An untagged 2024-01-01 becomes a Date object and is written back as 2024-01-01T00:00:00.000Z. Integers past 9007199254740991 are stored as doubles and silently lose their last digits.

Why comments, anchors and quoting style disappear

Nothing except content survives the trip, because the intermediate object has no place for it. Comments are dropped outright. Anchors are expanded, since the writer is called with noRefs: true, so a mapping anchored once and aliased once is emitted twice in full; a merge key written as << is already resolved during parsing. lineWidth: -1 disables the default 80-column folding, which keeps long unquoted strings on a single line. The writer is pinned to double quotes, so a quote it adds is never a single one. indent sets the space count at each nesting level and applies to block sequence items too, so at indent 2 the dash sits two spaces in from its key.

One document per paste, and how to read the errors

The component calls yaml.load, not loadAll, so a stream holding a second --- separator fails with "expected a single document in the stream, but found more". Positions in error messages are 1-based line:column, as in "tab characters must not be used in indentation (2:1)", and they point at the first offending character rather than the line that looks wrong. Duplicate keys inside one mapping are rejected instead of letting the last value win. Custom tags are unknown to the bundled schema, so !Ref reports "unknown tag !<!Ref>" and CloudFormation or Serverless templates cannot be validated here at all.

Worked example

Normalising a flow sequence and a four-space child mapping

Input
app: "DevToolbox"
release: "1.20"
ports: [8080, 8443]
env:
    DEBUG: false
Output
app: DevToolbox
release: "1.20"
ports:
  - 8080
  - 8443
env:
  DEBUG: false

The quotes on 1.20 must stay: without them the value re-parses as the number 1.2, whereas DevToolbox loses its quotes because no scalar type claims it. The flow sequence becomes a block sequence and the four-space child mapping is re-indented to two, both of which are writer defaults, not your input. Parsing the output again produces the same object.

Limits and pitfalls

  • Indent accepts 1–8 spaces in format mode and 0–8 in to-json mode; at 0 the JSON output collapses to one line, because JSON.stringify treats a zero indent as no pretty-printing.
  • Comments, anchors, merge keys and your original quoting or block-versus-flow choices are not restored, so a diff between input and output is layout noise rather than a record of your edits.
  • Only a single document is accepted; a file with another --- separator has to be split before pasting, which matters for multi-document Kubernetes bundles.
  • Custom tags such as !Ref or !Sub are not registered in the schema and are rejected as unknown tags, so CloudFormation and Serverless templates cannot be checked.
  • In to-json mode, untagged timestamps come out as ISO strings with milliseconds and integers beyond 9007199254740991 are rounded, so neither round-trips byte for byte.

Related tools

YAML formatter against the three things people reach for next

What you are doingThis pageTake the other route when
Reformatting a manifest you are hand-debuggingFormat mode parses the paste with js-yaml 4 and dumps it back at the indent you choose (1–8 spaces), with the result pane as read-only text to copy. Nothing is uploaded and no file on disk is touched.yq -P -i rewrites the file itself, one path per call, so a directory of manifests becomes a shell loop instead of forty pastes. Take it when the corrected file has to land in the repo rather than on the clipboard.
Tidying a .json fileJSON is a subset of YAML, so format mode accepts {"a":1} and re-emits it as block YAML, a: 1. That is a format conversion wearing a reformat label.The JSON formatter keeps the output readable by JSON.parse, fixes the indent at 2, 4 or 1 space, has a minify pane and can draft a TypeScript interface. Use it for anything downstream code must parse as JSON.
Producing minified JSONto-json mode runs JSON.stringify with an indent of 0–8; 0 collapses the whole document onto one line, which is the only minifier on this page.When the JSON already exists and only needs shrinking or re-indenting, the JSON formatter has a dedicated minify panel and its three indent choices, and you keep the TypeScript generation there.
The file parses but is still wrongValidate mode reports only what the parser rejects: bad indentation, duplicate keys, unknown tags. A Deployment missing apiVersion, or one with image: "", comes back as valid.yamllint for style and line-length rules, kubeconform or kubectl apply --dry-run=client for Kubernetes schema, cfn-lint for templates built on !Ref. Run these after this page says valid: it is a syntax gate, not a schema gate.
Two hundred files, or one very long pasteEvery keystroke in the textarea re-runs load plus dump synchronously on the main thread, with no debounce and no worker, so a huge paste is re-parsed while you type.A Node script that loads and dumps with js-yaml, or yq inside CI, handles the same batch unattended. Switch as soon as the work is repetitive instead of one-off.

One concrete scenario

Quote fsGroup and defaultMode before the reformat eats them

A manifest sets fsGroup: 0o755 and defaultMode: 0644 for three mounted files, and one line lists the mounts inline. Format mode parses both modes as numbers, so output carries 493 and 644: base 10 both times, with no marker saying the inputs were octal. The inline list takes the same trip, one line holding three items coming back as four, so the paste returns longer than it went in before a character of it changed. Quoting them as "0o755" and "0644" keeps them strings through the dump. Any config value whose leading zero or base carries meaning belongs in quotes before you press format.

Frequently Asked Questions

Why does YAML keep reporting indentation errors?
YAML expresses structure with indentation and is stricter than Python: spaces only (never tabs), and sibling nodes must align. Debugging tip: paste the suspicious region here and the validator points at the first broken line — 90% of the time a tab sneaked in or one level is off by a space.
When do values need quotes?
Whenever they contain special syntax: values starting with * & { } [ ] , # | > ! % @ ` , values containing ": ", leading/trailing spaces, and anything that should stay a string but looks like a number or boolean. Classic traps: 1.20 becomes the number 1.2, and NO becomes boolean false.
Can YAML and JSON be converted to each other?
Yes — YAML 1.2 is a superset of JSON. CI configs (GitHub Actions) and Kubernetes manifests frequently need round-trips; note that comments are lost converting to JSON, since JSON has no comment syntax.

Content reviewed: