JSON and YAML are the two most common data formats in modern software. JSON dominates APIs, database exports and most configuration files; YAML is the default for Kubernetes, Docker Compose, Ansible and most CI/CD pipelines. If your project ships either one, you will eventually have to convert, compare, or simply defend the choice. This guide compares them on syntax, expressiveness and failure modes, then tells you when each one genuinely wins.
What JSON gets right
JSON is a subset of JavaScript object literal syntax, so most JSON documents parse as valid JavaScript almost unchanged. It has exactly one number type, one string type, no comments, no custom tags and no anchors. That strictness is a feature: a JSON parser is only a few hundred lines of code, behavior is predictable across every language, and there is almost nothing that makes two JSON parsers disagree. That predictability is why OpenAPI specs, REST payloads and most event schemas default to JSON. Whitespace is insignificant, so an object such as {"name":"360tools Online","pages":120} is unambiguous whether it is minified or pretty-printed.
Where YAML shines
YAML was designed for humans who edit configuration, not for computers that exchange data. Its indentation-based structure removes braces and quotes: a scalar like port: 8080 needs no quotation at all, and a block that takes eight lines of JSON takes three lines of YAML. YAML also adds features JSON simply lacks: comments with #, multi-line strings via the pipe (|) and folded (>) block scalars, anchors and aliases that reuse a block, and custom tags such as !secret for tooling. When a file is read by people more often than by machines, YAML is usually the better home.
The syntax, side by side
| Feature | JSON | YAML |
|---|---|---|
| Comments | None | Supported with # |
| Multi-line strings | Require newline escapes | Block scalars with | and > |
| Indentation | Cosmetic only | Semantic — indentation is the structure |
| Tabs | Allowed outside strings | Forbidden as indentation |
| Dates | No date type; use ISO-8601 strings | Timestamp type in 1.1 parsers |
| Anchors / aliases | None | &name and *name reuse blocks |
| Failure mode | Loud: parsers reject bad input | Silent: valid YAML may not mean what you think |
Four differences that actually bite
- Implicit typing. In YAML, yes, on, off, null and 42 are all valid unquoted scalars, and 1.1-era parsers coerce several of them into booleans. JSON requires quotes, so the type is always explicit.
- Duplicate keys. Most JSON parsers silently keep the last duplicate; YAML 1.1 and 1.2 both reject them and most linters flag them.
- Dates. A YAML 1.1 parser turns 2024-05-10 into a date object, while JSON forces you to agree on an ISO-8601 string convention between systems.
- Anchors. YAML can define a block once and reuse it everywhere; JSON forces you to duplicate it, which is why generated configs balloon in size.
The Norway problem and other YAML gotchas
Two YAML gotchas cause most production incidents. The first is a stray tab in the indentation chain: it can throw a parse error or, worse, silently produce a document with a different structure than planned. The second is the classic Norway problem: in YAML 1.1, the country code no (for Norway) is interpreted as boolean false. Modern parsers default to 1.2, which fixed that, but plenty of legacy tooling still reads 1.1. When in doubt, quote scalars that resemble keywords.
Converting between the two
Converting by hand is error-prone, especially with nested objects and multi-line strings. The safe path is to let a converter do it. Paste JSON into the JSON to YAML Converter and get clean indented YAML back, or drop a YAML document in and get equivalent JSON. Run either input through the JSON Formatter first to confirm the structure at a glance. If you are converting because you are writing documentation, remember that many docs formats pair YAML front matter with Markdown — the Markdown-to-HTML converter turns the finished examples into publishable HTML.
Frequently asked questions
Should I switch an existing JSON API to YAML? No. JSON stays smaller on the wire, parses faster and has zero ambiguity — keep it for anything machine-to-machine. Use YAML for configuration humans will hand-edit and review.
Why does Kubernetes use YAML but other tools use JSON? Kubernetes was designed for a config-as-code workflow where people read deployments before approving them, so YAML's readability and comments win. Everything it exposes over HTTP is still JSON underneath.
Can a YAML parser read JSON? Yes. JSON is a subset of YAML 1.2, so a conformant 1.2 parser accepts most JSON documents as-is. The reverse is not true — YAML features like anchors and comments have no JSON representation.