Format Comparison
JSON vs YAML
JSON is the standard for APIs and data exchange. YAML is the standard for configuration files. They solve different problems — here's how to choose, and what to watch out for in each.
The same data, two formats
A Kubernetes-style deployment config in both formats.
{
"name": "api-server",
"replicas": 3,
"image": "nginx:1.25",
"env": {
"LOG_LEVEL": "info",
"PORT": "8080"
},
"ports": [80, 443],
"healthCheck": {
"path": "/health",
"intervalSeconds": 30
}
}name: api-server replicas: 3 image: nginx:1.25 env: LOG_LEVEL: info PORT: "8080" # quoted to keep as string ports: - 80 - 443 healthCheck: path: /health intervalSeconds: 30 # YAML allows comments — JSON does not
Feature comparison
| Feature | JSON | YAML |
|---|---|---|
| Syntax | Braces, brackets, double-quoted strings | Indentation, colons, minimal punctuation |
| Comments | Not supported | Supported (# inline or block) |
| Readability | Verbose for humans, unambiguous for parsers | Very readable, but whitespace-sensitive |
| Multiline strings | Requires \n escapes | Literal blocks (|) and folded blocks (>) |
| Data types | String, number, boolean, null, array, object | Same, plus timestamps, binary, and more |
| Boolean gotchas | true / false only | yes/no/on/off also parse as booleans (YAML 1.1) |
| Null | null | null, ~, or empty value |
| Numbers | No octal/hex literals | 0o755, 0xFF valid (YAML 1.1) |
| Parser speed | Fast — format designed for machines | Slower — ambiguous grammar requires more work |
| Spec stability | Stable (RFC 8259) | YAML 1.1 vs 1.2 differences cause parser bugs |
| Superset? | JSON is valid YAML 1.2 | YAML is a superset of JSON (1.2 only) |
| Primary use | APIs, data exchange, web apps | Config files, CI/CD, Kubernetes, Docker Compose |
JSON — use it when
REST and GraphQL APIs
Every HTTP client in every language can parse JSON. It's the de-facto wire format for APIs. YAML is almost never used for API responses.
Data storage and exchange
JSON is the right format when data moves between systems — databases, pipelines, queues. Its strictness (no comments, no implicit types) makes it unambiguous.
Browser and JavaScript contexts
JSON is native to JavaScript — JSON.parse() and JSON.stringify() are built in. YAML has no native browser support.
High-throughput systems
JSON parsers are faster than YAML parsers because JSON's grammar is simpler. At scale — millions of records — that difference matters.
YAML — use it when
Configuration files
Kubernetes manifests, GitHub Actions workflows, Docker Compose, Ansible playbooks — all use YAML. Its readability and comment support make it the right choice when humans edit files frequently.
Files that need comments
JSON has no comment syntax. If you need to annotate configuration values with explanations, YAML is the only option between the two.
Multiline strings
YAML's literal block scalar (|) and folded block scalar (>) make multiline content readable. SQL queries, shell scripts, and long descriptions are far cleaner in YAML than in JSON with escaped newlines.
Human-edited config with complex structure
Deep nesting is more readable in YAML's indentation-based syntax than in JSON's nested braces. For files that developers read and edit daily, YAML reduces cognitive overhead.
YAML gotchas to know
YAML's flexibility creates edge cases that JSON's strictness avoids entirely.
The Norway problem
In YAML 1.1, the two-letter country code "NO" and the string "no" parse as boolean false. Same for "yes", "on", "off". This famously broke Norwegian locale configs. YAML 1.2 fixed this, but most parsers still default to 1.1 behavior.
# YAML 1.1 — these are booleans, not strings enabled: yes # true country: NO # false (!) toggle: on # true # Fix: quote the values country: "NO" toggle: "on"
Implicit type coercion
YAML infers types without explicit markers. Version numbers, port numbers, and semantic version strings can all be misinterpreted.
# YAML parses these without quotes: version: 1.0 # float 1.0, not string "1.0" port: 8080 # integer 8080 tag: v1.2.3 # string (fine here) octal: 0755 # integer 493 in YAML 1.1 # Safer with quotes: version: "1.0" port: "8080"
Indentation errors are silent
A single wrong indent in YAML changes the structure without producing an error. A list item indented one extra space becomes a nested object. JSON's explicit braces make these mistakes obvious immediately.
# Intended: list of strings
tags:
- billing
- admin
# Accidental extra indent — now it's a nested object
tags:
- billing # This is a list under a null key, not tagsFrequently asked questions
Is JSON valid YAML?
JSON is valid YAML 1.2 but not necessarily YAML 1.1. Most modern YAML parsers (Go's gopkg.in/yaml.v3, Python's ruamel.yaml, JavaScript's js-yaml 4+) implement 1.2 and will parse JSON correctly. Older parsers using 1.1 may trip on JSON with "true"/"false" values or specific number formats.
Can I convert JSON to YAML without losing data?
Yes for round-trips in most cases. JSON's type system is a subset of YAML's, so JSON→YAML is always safe. YAML→JSON can lose comments (JSON has none) and may require quoting values that YAML represents without quotes (like version numbers and country codes).
Why does Kubernetes use YAML instead of JSON?
Kubernetes manifests are written by humans, edited frequently, and benefit from comments explaining why a value is set. YAML's readability and comment support made it the natural choice for config that developers interact with daily. Kubernetes actually stores objects as JSON internally — YAML is just the authoring format.
Which is faster to parse — JSON or YAML?
JSON is significantly faster. JSON's grammar is context-free and unambiguous. YAML's grammar is context-sensitive (indentation level changes meaning) and requires more lookahead. Benchmarks consistently show JSON parsing 5–10× faster than YAML for equivalent data.
Does YAML support arrays and nested objects like JSON does?
Yes. YAML's sequence (- item) maps to JSON arrays, and YAML's mapping (key: value) maps to JSON objects. They're semantically equivalent. YAML also has flow style ({key: value} and [a, b]) which is identical to JSON syntax.
Need to convert between JSON and YAML?