JSON vs YAML: When to Use Each Format
JSON Overview
JSON (JavaScript Object Notation) was formalized in RFC 4627 (2006) and is now defined by RFC 8259. It supports six data types: strings, numbers, booleans, null, arrays, and objects. JSON is a strict subset of JavaScript, but is language-independent and supported natively in virtually every programming environment.
{
"name": "example",
"version": "1.0.0",
"active": true,
"scores": [98, 87, 92],
"metadata": {
"created": "2026-01-01",
"tags": ["production", "stable"]
}
}
YAML Overview
YAML (YAML Ain't Markup Language) is a superset of JSON — every valid JSON document is valid YAML. YAML 1.2 (2009) tightened the specification to make this true formally. YAML adds comments, multi-line strings, type coercion, anchors, aliases, and a human-friendly syntax without mandatory quotes and braces.
name: example
version: "1.0.0"
active: true
scores:
- 98
- 87
- 92
metadata:
created: "2026-01-01"
tags:
- production
- stable
Syntax Comparison
| Feature | JSON | YAML |
|---|---|---|
| Comments | Not supported | # inline comments |
| Multi-line strings | Escape \n | Literal (|) and folded (>) blocks |
| Quotes | Strings must be quoted | Quotes optional for most strings |
| Null | null | null, ~, or empty value |
| Booleans | true, false | true, yes, on, false, no, off |
| Anchors / reuse | Not supported | &anchor and *alias |
When to Use JSON
- APIs — JSON is the lingua franca of REST and GraphQL APIs. Parse overhead is lower and tooling is universal.
- Configuration read by code — if a program reads the config, strict parsing with no implicit type coercion is safer.
- Package manifests —
package.json,composer.json, and similar files are JSON by convention. - Data interchange between services — deterministic serialization with no hidden whitespace significance.
When to Use YAML
- Configuration written by humans — Kubernetes manifests, GitHub Actions, Docker Compose, and Ansible all use YAML because it's more readable with less punctuation.
- Multi-line content — embedding scripts, SQL queries, or prose in config files is natural with YAML's block scalars.
- Anchors and aliases — reusing repeated values across a large config file is only possible in YAML.
# YAML anchor example
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com
staging:
<<: *defaults
host: staging.example.com
YAML Pitfalls
YAML's flexibility creates several well-known traps:
The Norway Problem
In YAML 1.1, unquoted no is parsed as boolean false. A list of country codes including Norway (NO) would silently become false. YAML 1.2 fixed this, but many parsers still implement 1.1. Quote strings when in doubt.
# YAML 1.1 — "no" becomes false
countries:
- US
- UK
- no # parsed as false, not "no"
# Safe
countries:
- US
- UK
- "no"
Indentation sensitivity
YAML uses indentation for structure, like Python. A single wrong tab character (YAML prohibits tabs) or misaligned space causes parse errors or silently changes the data structure.
Implicit type coercion
Unquoted values like 1.0 become floats, 0755 can become octal integers, and 2026-01-01 becomes a date object in some parsers. Use explicit quotes for values you intend to be strings.
Frequently Asked Questions
Is YAML faster to parse than JSON?
No. JSON parsing is significantly faster. JSON's restricted grammar allows simpler, faster parsers. YAML parsers must handle implicit typing, anchors, and multi-document streams, making them more complex. For high-throughput APIs, always prefer JSON.
Can I convert between JSON and YAML losslessly?
JSON to YAML is lossless — YAML is a superset. YAML to JSON may lose comments, anchors, and multi-line string formatting (the data is preserved, but the presentation isn't). Use the JSON to YAML converter or YAML to JSON converter for quick conversions.
Which should I use for CI/CD configuration?
Most CI/CD platforms use YAML: GitHub Actions (.github/workflows/*.yml), GitLab CI (.gitlab-ci.yml), CircleCI, Azure Pipelines. The choice is usually made for you by the platform. For custom tooling configuration files where you have a choice, prefer YAML for human-edited config and JSON for machine-generated config.
Related Tools