JSON vs YAML:
When to Use Which
Pick Based on the Reader
Use JSON when machines are the primary reader: APIs, browser storage, and data interchange. It’s strict, predictable, and supported everywhere.
Use YAML when humans are the primary reader: configuration files, pipelines, and templates. It’s concise and supports comments, but it’s easier to break with whitespace mistakes.
# In this article
1. The Core Difference
Both JSON and YAML represent the same kinds of structures: objects/maps, lists/arrays, strings, numbers, booleans, and null. The difference is mainly syntax and how forgiving the format is.
JSON
{
"name": "OctalOne",
"features": ["offline", "private"],
"enabled": true,
"limits": { "maxMb": 50 }
}YAML
name: OctalOne features: - offline - private enabled: true limits: maxMb: 50
Quick fact: YAML is a superset of JSON — valid JSON can also be valid YAML. In practice, though, people rarely write JSON inside YAML files.
2. Readability vs Strictness
JSON is strict by design: quotes, braces, commas. That strictness makes it hard to misread and easy to parse consistently.
YAML optimizes for humans: fewer symbols, indentation-based structure, and natural-looking scalars. The trade-off is that whitespace errors (or unclear intent) can slip in.
Rule of thumb:
- Choose JSON when reliability and interoperability matter most.
- Choose YAML when editing by humans is the primary workflow.
4. Data Types & Surprises
JSON has a small, predictable set of types. YAML supports more features and has some parser-dependent behaviors. That power can be useful — but it can also surprise you.
Common YAML pitfalls
- Indentation matters: tabs vs spaces or misaligned blocks can change meaning.
- Unquoted scalars: values like
on,off,yesmay be interpreted as booleans by some parsers. - Duplicate keys: behavior varies (some parsers override silently).
If you need “always the same everywhere” behavior, JSON typically wins. If you need advanced config features (like multi-line strings or anchors), YAML can be worth it.
5. Tooling & Ecosystem
JSON is native to the web: browsers, JavaScript, and most APIs. It’s the default language for REST payloads and many storage formats.
YAML dominates config: Kubernetes manifests, CI pipelines, docker-compose, and many infra tools. It’s designed to be edited by humans and reviewed in pull requests.
| Format | Best for | Human editing | Machine parsing |
|---|---|---|---|
| JSON | APIs, interchange, storage | Okay | Excellent |
| YAML | Configs, pipelines, manifests | Excellent | Good (but beware edge-cases) |
6. Verdict (Simple Rules)
- Use JSON for public contracts: APIs, SDKs, events, logs.
- Use YAML for human-authored configuration that benefits from comments.
- If you choose YAML, add validation (schema/linting) in CI to catch whitespace and type surprises.
Work with JSON (Offline)
Beautify, validate, and clean up JSON directly in your browser.
3. Comments & Trailing Commas
YAML supports comments. JSON does not (in the official spec), which is why configuration files often prefer YAML.
YAML supports comments
# Build pipeline config image: node:20 steps: - name: install run: npm ci # deterministic installsJSON has no comments
{ // Not valid JSON "image": "node:20" }Another classic JSON gotcha: no trailing commas. YAML is more flexible in places, but can still be strict depending on the parser.