JSON and YAML are the two most popular data serialization formats in modern software. Both can represent the same data structures, but they look completely different and excel in different situations. This guide breaks down everything you need to know to make the right choice.
The same data in both formats
JSON:
{
"server": {
"host": "localhost",
"port": 8080,
"debug": true
},
"database": {
"name": "mydb",
"pool_size": 5
}
}
YAML:
server:
host: localhost
port: 8080
debug: true
database:
name: mydb
pool_size: 5
Same data, very different look. The YAML version has no braces, no quotes, and no commas — just indentation.
Key differences at a glance
- Syntax: JSON uses braces
{}and brackets[]. YAML uses indentation. - Comments: YAML supports
#comments. JSON does not. - Verbosity: YAML is typically 20–40% shorter than equivalent JSON.
- Data types: YAML has more native types (dates, nulls with
~, binary). JSON has fewer. - Strictness: JSON is strict and unambiguous. YAML has many implicit conversions that can cause surprises.
- Speed: JSON parses significantly faster than YAML.
When to use JSON
- REST APIs — JSON is the universal language of APIs. Every HTTP client can parse it.
- Data storage — MongoDB, DynamoDB, PostgreSQL JSONB all speak JSON natively.
- JavaScript frontends —
JSON.parse()is built into every browser. - Performance-critical systems — JSON parsers are faster and more battle-tested.
- When sharing with external systems — JSON is universally understood.
When to use YAML
- Kubernetes manifests —
kubectlexpects YAML. - Docker Compose —
docker-compose.ymluses YAML. - GitHub Actions — all workflow files are YAML.
- Configuration files — YAML's comment support and readability make it ideal for configs humans edit regularly.
- Ansible playbooks — the entire Ansible ecosystem runs on YAML.
The YAML Norway problem
YAML's implicit type conversion is a famous source of bugs. The classic example:
countries:
- GB
- US
- NO # Norway — but YAML interprets this as boolean false!
In older YAML parsers, NO, Yes, On, Off were parsed as booleans. This was fixed in YAML 1.2, but many parsers still use 1.1 behavior. JSON has no such ambiguity.
Is YAML a superset of JSON?
Technically yes — any valid JSON is also valid YAML (YAML 1.2+). But in practice they are used very differently. You wouldn't write a Kubernetes manifest in JSON even though it works.
Try it free — JSON ↔ YAML Converter
Convert between JSON and YAML instantly in your browser. No upload, no signup.
Quick decision guide
- Building an API? → JSON
- Writing a config file humans will edit? → YAML
- Kubernetes, Docker, GitHub Actions? → YAML
- Storing data in a database? → JSON
- Need comments in the file? → YAML
- Parsing speed matters? → JSON