What They Have in Common
Both JSON and YAML represent structured data as key-value pairs, arrays, strings, numbers, booleans, and null. Any valid JSON document is also valid YAML — YAML is a superset of JSON. The differences are in syntax, readability, and ecosystem support.
JSON: Strict, Verbose, Universal
{
"database": {
"host": "localhost",
"port": 5432
},
"features": ["auth", "billing"]
}
JSON requires quotes around all keys, commas between items, and has no comment syntax. This verbosity is also its strength: every JSON parser in every language produces the same result with no parsing surprises.
YAML: Readable but Fragile
database:
host: localhost
port: 5432
features:
- auth
- billing
YAML eliminates quotes and braces, replacing them with indentation. It supports comments with #, multi-line strings, and anchors for reusing values — making complex configuration files far more readable.
YAML Pitfalls
- The Norway problem:
country: NOis parsed as boolean false in YAML 1.1 parsers - Indentation sensitivity: Mixing tabs and spaces produces parse errors that are hard to debug
- Implicit types:
port: 5432is an integer;enabled: yesis a boolean — all without quotes
When to Use Each
| Use Case | Recommendation |
|---|---|
| API responses / requests | JSON — universal parser support |
| Config files humans edit | YAML — comments, readability |
| CI/CD pipelines (GitHub Actions) | YAML — industry standard |
| Kubernetes manifests | YAML — ecosystem default |
| Storing data in databases | JSON — easier to query with SQL JSON functions |
Practical Rule
Use JSON when machines write and read the data. Use YAML when humans write and machines read it. The comment support in YAML is genuinely valuable for configuration files — being able to explain why a setting exists is worth the indentation trade-off.