JSON vs YAML: When to Use Each
How JSON and YAML relate, their syntax differences, the type-inference gotchas that bite people, and a safe way to convert between them.
Updated July 2026 · 6 min read
JSON and YAML describe the same kind of data — maps, lists, and scalar values — but they are tuned for different audiences. JSON is precise and universal, ideal for machines. YAML is airy and comment-friendly, ideal for humans writing configuration. Knowing which to reach for (and how to convert cleanly) saves a lot of friction.
The same data, two syntaxes
Here is a small config in JSON:
{
"service": "api",
"replicas": 3,
"ports": [8080, 8443],
"env": { "LOG_LEVEL": "info" }
}And the identical data in YAML:
service: api
replicas: 3
ports:
- 8080
- 8443
env:
LOG_LEVEL: infoYAML drops the braces, brackets, and most quotes, using indentation to express nesting and dashes for list items. Because any valid JSON is also valid YAML, a YAML parser can read the JSON version directly.
Key differences
| JSON | YAML | |
|---|---|---|
| Structure | Braces and brackets | Indentation |
| Comments | Not allowed | # comments |
| Quotes | Required on keys and strings | Usually optional |
| Best for | APIs, data exchange | Config, pipelines, manifests |
| Readability | Dense | Airy, human-friendly |
Choosing the right one
- Reach for JSON when programs are the reader and writer: REST request and response bodies, browser
localStorage, message queues, and anywhere strictness prevents bugs. - Reach for YAML when people maintain the file by hand: GitHub Actions and other CI pipelines, Docker Compose, Kubernetes manifests, and Ansible playbooks — places where comments and readability pay off.
YAML's type-inference gotchas
YAML's convenience has a sharp edge: it guesses types from bare values, which occasionally surprises you.
- Booleans in disguise.
yes,no,on, andoffcan be read as booleans. The famous "Norway problem" is a country-code list whereNOsilently becomesfalse. - Numbers that should be strings. A version like
3.10may be parsed as the number3.1, dropping the trailing zero. Quote it:"3.10". - Tabs. YAML forbids tabs for indentation — use spaces only, and be consistent.
When in doubt, quote the value. Explicit quoting removes ambiguity and prevents type-inference surprises.
Converting safely
- Validate the source first — malformed JSON or YAML will not convert cleanly.
- Convert, then read the output to confirm structure and types survived.
- Re-quote any value that must stay a string (versions, country codes, leading-zero numbers).
- Add comments back into YAML if you converted from JSON, since JSON cannot carry them.
Try it
The DevBox JSON ⇄ YAML Converter translates in both directions instantly, and the JSON Formatter helps you validate the JSON side before you convert. For the full picture on JSON itself, read the JSON formatting guide.
Frequently asked questions
Is YAML a superset of JSON?+
Effectively yes. Any valid JSON is also valid YAML, so a YAML parser can read JSON directly. YAML adds indentation-based syntax, comments, and other conveniences on top.
When should I use YAML instead of JSON?+
Use YAML for configuration that humans write and read — CI pipelines, Docker Compose, Kubernetes manifests — where comments and readability matter. Use JSON for data exchanged between programs, such as API requests and responses.
Why is indentation so important in YAML?+
YAML uses indentation to express structure instead of braces and brackets. Inconsistent spaces, or tabs mixed with spaces, change the meaning or cause parse errors. Always use spaces, never tabs.
Does converting JSON to YAML lose any data?+
No. Both represent the same data model of maps, lists, and scalars, so a round trip preserves your values. Only formatting (comments, key order in some cases, and quoting style) may differ.
Why did my YAML value become true or a number unexpectedly?+
YAML infers types, so bare words like yes, no, on, off, and version numbers can be read as booleans or numbers. Quote the value ("3.10") when you need it to stay a string — a classic 'Norway problem' (NO becoming false).