How to Convert JSON to YAML (and Back) Correctly
A guide to converting between JSON and YAML: how the data models map, multi-line strings and anchors, the gotchas that bite, and how to convert both directions without corrupting your config.
YAML is the lingua franca of modern config — Kubernetes, Docker Compose, GitHub Actions — while JSON is the format APIs and code speak. Moving between them is a routine task: convert a JSON API response into a readable YAML config, or turn a hand-edited YAML file into JSON your code can parse. This guide explains how the conversion works, the edge cases that cause silent corruption, and how to do it safely in both directions.
Why convert between JSON and YAML
YAML is a superset of JSON in spirit: it can represent the same maps, lists, and scalars, but with indentation instead of braces and far less punctuation. Developers convert JSON to YAML to make machine-generated data comfortable to read and edit, and YAML to JSON to feed human-authored config into tools and code that expect strict JSON. Converting YAML to JSON is also a quick way to validate it with the broader JSON toolchain.
How the data models map
{ "service": "api", "ports": [80, 443], "tls": { "enabled": true } }service: api
ports:
- 80
- 443
tls:
enabled: true- A JSON object becomes a YAML mapping (key: value pairs by indentation)
- A JSON array becomes a YAML sequence (dash-prefixed list items)
- JSON strings, numbers, booleans, and null map to the corresponding YAML scalars
- Nesting that JSON expresses with braces and brackets, YAML expresses with indentation
Multi-line strings and readability
One of YAML's biggest wins over JSON is multi-line strings. JSON must cram long text onto one line with escaped newline sequences; YAML offers literal and folded block scalars that keep multi-line content readable. When converting JSON to YAML, a good converter can render long string values as block scalars, which is a major reason the YAML version is easier to maintain.
Anchors, aliases, and features JSON lacks
YAML has features with no JSON equivalent: anchors and aliases for reusing a block, comments, and multiple documents in one file separated by triple dashes. When you convert YAML to JSON, these are flattened or dropped — aliases are expanded to their full value, comments disappear, and only one document survives unless handled explicitly. Going from JSON to YAML never invents these features; it produces straightforward YAML you can then enhance by hand.
Gotchas that cause silent corruption
country: NO # parsed as the boolean false!
country: "NO" # correct: the string "NO"
zip: "01234" # quote to keep the leading zero- The Norway problem — unquoted NO, YES, ON, OFF can be read as booleans in YAML; quoting preserves them as strings.
- Numeric-looking strings — a ZIP code or version like 1.10 may lose a trailing zero or be read as a number unless quoted.
- Comments are lost when converting YAML to JSON, since JSON cannot represent them.
- Indentation errors — YAML is whitespace-sensitive, so a converter must emit consistent indentation to stay valid.
Converting safely in both directions
For a reliable round trip, convert with a tool that parses and validates rather than doing find-and-replace on text. The JSON to YAML tool turns API payloads and data into readable YAML, and the YAML to JSON tool turns config into strict JSON for code and validation. After converting YAML to JSON, run the result through a validator or formatter to confirm it parsed as you expected and to inspect the structure.
Frequently asked questions
Is YAML a superset of JSON?
Effectively yes — YAML can represent the same data as JSON, and most JSON is also valid YAML. The reverse is not true: YAML has features (anchors, comments, multiple documents) that JSON cannot represent, so converting YAML to JSON can lose information.
Will I lose comments converting YAML to JSON?
Yes. JSON does not support comments, so any comments in a YAML file are dropped during conversion. Keep the YAML file as your editable source if the comments matter.
Why did my YAML value change type after conversion?
YAML's loose type inference can read unquoted values like NO or 1.10 as booleans or numbers. Quote strings that could be misinterpreted before converting, so they round-trip as the strings you intended.
How do I convert JSON to YAML online?
Paste your JSON into a JSON-to-YAML converter that parses and re-serializes the data. A client-side tool keeps your data in the browser and can render long strings as readable block scalars.
Can I convert YAML back to JSON without data loss?
Plain maps, lists, and scalars convert cleanly. Features unique to YAML — comments, anchors and aliases, and multi-document files — are flattened or dropped, so those specific constructs do not survive the round trip.
Related tools
Try these tools
JSON to YAML Converter
Convert JSON to YAML for configs, Kubernetes, and CI pipelines.
YAML to JSON Converter
Convert YAML configs and manifests into structured JSON.
JSON Validator
Validate JSON instantly with precise, human-readable error messages and line numbers.
JSON Formatter & Beautifier
Format, prettify, and beautify messy JSON with configurable indentation and key sorting.