JSON vs YAML: what’s the difference?
Both JSON and YAML are used to represent structured data. JSON is strict and universal; YAML is more human-friendly but easier to get wrong.
Quick comparison
- Best for machines: JSON
- Best for humans editing configs: YAML (carefully)
- Strictness: JSON is strict; YAML has more “magic” and implicit typing
- Comments: YAML supports comments; JSON does not (by default)
Example: same data
JSON:
{
"service": "api",
"port": 8080,
"enabled": true,
"tags": ["prod", "us-east-1"]
}
YAML:
service: api
port: 8080
enabled: true
tags:
- prod
- us-east-1
Common YAML pitfalls
- Indentation: spaces matter; inconsistent indentation breaks parsing.
- Implicit types: strings like
on, off, or dates can be interpreted unexpectedly by some parsers.
- Tabs: many YAML parsers reject tabs.
When to choose JSON
- APIs and network payloads
- Interoperability across languages and systems
- When you want strict validation and predictable parsing
When to choose YAML
- Configuration files edited by humans
- When comments are important
- When readability matters more than strictness
Related: Validate JSON · Pretty print JSON