JSON and YAML are the two dominant data serialization formats in modern software. JSON rules the web. YAML rules DevOps. But they overlap, and choosing between them matters.
Syntax comparison
The same data, two ways:
JSON:
{
"name": "anan.zh.kg",
"version": 1,
"tools": ["uuid", "base64", "json"],
"active": true,
"maintainer": {
"name": "deng",
"email": "hi@anan.zh.kg"
}
}
YAML:
name: anan.zh.kg version: 1 tools: - uuid - base64 - json active: true maintainer: name: deng email: hi@anan.zh.kg
When to use JSON
- APIs and web services — universal browser and language support
- Data interchange — the lingua franca of HTTP, message queues, and storage
- Schemas — paired with JSON Schema for validation
- When speed matters — JSON parsers are 5-10x faster than YAML parsers
When to use YAML
- Configuration files — Kubernetes, GitHub Actions, Ansible, Docker Compose, CI pipelines
- Human-edited data — comments, multi-line strings, anchors are YAML strengths
- Documentation — OpenAPI specs, Swagger, Postman exports
- When readability matters more than speed
YAML features JSON cannot match
- Comments —
# this is a comment - Anchors and aliases — DRY:
&anchor *ref - Multi-line strings —
|literal,>folded - References to other documents — multi-file configs
- No quotes required — cleaner for most strings
JSON features YAML cannot match
- Speed — JSON is 5-10x faster to parse and serialize
- Strictness — no Norway problem, no truthy string surprises
- Universal support — every language, every browser, every tool
- Smaller output — 10-30% less whitespace
The Norway problem
YAML 1.1 had a notorious surprise: no is parsed as boolean false. So country: Norway became {country: false}. YAML 1.2 (2009) fixed this, but many parsers default to 1.1 for compatibility. Always quote your strings when in doubt: country: "Norway". See the official YAML 1.2 spec for details.
Practical advice
Use JSON for any data that crosses a network boundary or is consumed by a program.
Use YAML for configuration files written and maintained by humans.
If you need both, the YAML spec has a !!json tag to embed JSON inline, and most JSON files are valid YAML (with caveats around comments and strictness).
Format your JSON with our JSON Formatter.
Frequently Asked Questions
Q: Is JSON always faster than YAML?
Yes. JSON parsers are typically 5-10x faster than YAML parsers because JSON's strict grammar is simple to parse deterministically, while YAML's flexible syntax requires more complex state machines. For high-performance contexts like API gateways or real-time data pipelines, JSON is the better choice.
Q: What is the Norway problem in YAML?
YAML 1.1 parsed 'no' as boolean false, so 'country: Norway' became '{country: false}'. YAML 1.2 (2009) fixed this, but many parsers still default to 1.1. Always quote your strings when in doubt. The YAML 1.2.2 spec explains the change.
Q: Can you embed JSON inside a YAML file?
Yes! Most JSON documents are valid YAML, and the YAML spec includes a !!json tag for explicit JSON embedding. Many tools like OpenAPI and Terraform accept both formats for the same configuration.