Convert YAML to JSON (and Avoid Indentation Errors)

Since YAML 1.2 every JSON document is valid YAML. Convert YAML config to JSON, and watch for indentation and type-inference traps.

YAML and JSON are related

YAML 1.2 is a strict superset of JSON, so any JSON document is already valid YAML. Converting YAML to JSON mainly means turning indentation-based structure into braces and brackets.

Indentation rules

YAML forbids tab characters for indentation — use spaces, and keep sibling keys at the same depth. A stray tab or a misaligned key is the most common YAML parse error.

The Norway problem

Unquoted NO, yes, on, and off can be read as booleans by some parsers, so the country code NO becomes false. Quote such values to force them to stay strings.

Converting safely

Validate the YAML first, then convert. Quote ambiguous scalars and confirm that numbers, dates, and leading-zero values survive as the type you expect.

Multi-document YAML streams

A single YAML file can hold multiple documents separated by '---'. JSON has no equivalent — convert each document independently and wrap them in a JSON array if a downstream tool needs all of them in one value. Kubernetes manifests are the most common case.

Anchors and aliases

YAML's & anchor and * alias let you reuse a node by reference, but JSON has no aliases. A safe converter resolves each alias to a copy of the anchored value, which expands the document. Be aware that round-tripping back to YAML loses the original sharing.

When YAML loses fidelity

Comments, tag annotations like !!binary, custom tags, the difference between block and flow style, and key order are all dropped when YAML becomes JSON. For pure data interchange that is fine; for human-edited config that gets edited again later, keep the YAML as the source of truth.

See also

This guide handles one format boundary. The hub lists every JSON ↔ neighbor-format conversion with its standard and edge cases.