← Guides

JSON vs YAML: Comments, Indentation and Hidden Type Traps

Same data model, different readers

JSON and YAML describe the same primitives — objects, arrays, strings, numbers, booleans and null — but they target different audiences. JSON is strict and compact: braces, brackets, double-quoted keys, no comments. It is the natural wire format for APIs. YAML trades punctuation for indentation, allows comments, and is pleasant to edit by hand, which is why Kubernetes manifests, CI pipelines and application configuration files use it.

Comments exist only in YAML

A YAML # comment is not data. Parsers discard it, and JSON has no syntax for it, so any comment disappears the moment you convert to JSON — there is nowhere to store it. Keep comments by keeping the YAML source authoritative and converting a copy for tools that demand JSON.

Indentation is structure

In YAML, indentation defines nesting. Two spaces are the common convention; tabs are not allowed as indentation and produce parse errors. A misplaced space does not throw in every case — sometimes it quietly changes a mapping into a string, which is far worse than an error. Editors with YAML schema awareness catch most of this.

Type traps

YAML has no quoting requirement for simple strings, which is where the danger lies:

  • Version numbers — version: 1.0 parses as a float, so 1.10 becomes 1.1
  • zip: 01234 — leading zeros may be read as octal in YAML 1.1
  • color: no — YAML 1.1 treats no/off/yes/on as booleans; YAML 1.2 does not
  • time: 12:30 — a sexagesimal integer in YAML 1.1

The safe habit is to quote any scalar that is meant to be a string but could be read as something else. Version numbers are the classic case: "1.10" as a float becomes 1.1 and reorders your version comparison.

Anchors, aliases and merge keys

YAML supports &anchor definitions and *alias references, plus << merge keys for inherited defaults. They reduce duplication in long configs, but they are not data: converting to JSON expands every reference into a repeated value. The result is correct and much larger. Some JSON consumers, and most diff tools, will no longer show that two blocks were intentionally identical.

Multiple documents

A YAML stream can contain several documents separated by ---. JSON can hold exactly one value, so a multi-document file cannot become a single JSON document. Split it first, or wrap the documents into an array deliberately.

Numbers and large integers

JSON numbers are IEEE 754 doubles in most parsers, so integers beyond 253 lose precision — a 64-bit ID becomes wrong when read into JavaScript. YAML has the same practical limits once it reaches a JSON-based runtime. If identifiers are large, store them as strings on both sides.

Round-tripping is not byte-perfect

Converting YAML to JSON and back changes formatting: key order may move, quoting style normalises, anchors expand, and comments are gone. Treat the conversion as a data transformation, not a format-preserving edit. Review the diff before committing a converted config.

Choosing a format

Use JSON when machines are the primary reader: HTTP APIs, published data, anything with a strict schema. Use YAML when humans edit the file regularly, comments carry meaning, or the ecosystem expects it — Kubernetes, GitHub Actions, Docker Compose, Ansible. When a tool needs both, generate one from the other rather than maintaining two copies by hand.

YAML is a superset of JSON, with caveats

Since YAML 1.2, any valid JSON document is also valid YAML, which is why some pipelines accept either. The reverse is not true: YAML adds comments, anchors, block scalars, tags and multiple documents, none of which JSON can express. Two practical differences bite in mixed files. YAML forbids tab indentation, so a JSON snippet pasted with tabs may parse in one place and fail in another. And YAML 1.1 parsers read unquoted yes and no as booleans, so a value that is a string in JSON can silently change type when the same text is treated as YAML.

Loading YAML safely

YAML can describe objects as well as data. Older loaders in Python, Ruby and Java would instantiate arbitrary classes named in the document, which turns a config file from an untrusted source into remote code execution. Use the safe loader — yaml.safe_load rather than yaml.load — and keep tags disabled unless you control the input. Alias expansion is the other risk: a small file can expand into gigabytes of repeated references, so limit anchors and aliases when parsing uploads. JSON parsers have a narrower surface, which is one more reason to prefer JSON at a trust boundary.

Convert in either direction with our JSON ⇄ YAML converter. It offers two- and four-space indentation, reports parse errors with the offending line, and runs entirely in your browser.

Try it yourself: open the JSON ⇄ YAML Converter →