YAML vs JSON: Differences and When to Use Each
Abhay Khant
Jan 1, 1970 • 4 min read
YAML vs JSON: Differences and When to Use Each
- JSON: machine-first, strict, universal; YAML: human-first, expressive, whitespace-driven
- YAML supports comments and anchors; JSON supports neither
- JSON parses faster and fails loudly; YAML fails on subtle indentation
- Default split: YAML for configs humans edit, JSON for machine interchange
The core difference between YAML and JSON
The yaml vs json comparison starts with an irony: YAML is technically a superset of JSON, so every valid JSON document is also valid YAML. Despite that family relation, the two formats serve different masters. [JSON](https://www.json.org/json-en.html) was designed as a data interchange format for machines: strict syntax, minimal surface area, trivially parseable everywhere. [YAML](https://yaml.org/spec/1.2.2/) was designed for humans writing configuration: indentation instead of brackets, comments allowed, and features that reduce repetition at the cost of parsing complexity.
That origin difference predicts every practical distinction below, from error behavior to ecosystem support.
The same data side by side
{
"server": {
"host": "0.0.0.0",
"port": 8080,
"debug": false
},
"features": ["auth", "logging"]
}
server:
host: 0.0.0.0
port: 8080
debug: false
features:
- auth
logging
Same structure, different feel. The YAML version drops braces, quotes, and commas in favor of indentation, which reads faster and edits cleaner by hand. The JSON version survives any formatting mangling that preserves its punctuation; the YAML version breaks silently if indentation shifts.
Feature-by-feature comparison
| Feature | JSON | YAML |
|---|---|---|
| Comments | No | Yes (#) |
| Anchors and references | No | Yes (& and *) |
| Multiline strings | Escaped \n only | Literal and folded blocks |
| Type coercion surprises | Minimal | The Norway problem: no becomes false |
| Parse speed | Faster | Slower (complex grammar) |
| Error visibility | Loud, line-numbered | Sometimes silent misinterpretation |
| Ecosystem ubiquity | Nearly universal | Config-heavy ecosystems |
The Norway problem: where YAML bites
YAML's most famous gotcha has its own name. In YAML 1.1, the two-letter country code NO parses as boolean false, along with off, unquoted version numbers like 1.20 collapsing to 1.2, and similar type-coercion surprises. A European country list or a version string can silently become something else entirely. JSON's stricter quoting rules make such coercion impossible: values are what you wrote or the parse fails loudly. Defensive YAML practice quotes anything ambiguous, which erodes some of YAML's brevity advantage.
Where each format wins
- Configuration files humans edit: YAML. [Kubernetes](https://kubernetes.io/docs/concepts/configuration/overview/) manifests, [GitHub Actions](https://docs.github.com/en/actions) workflows, and Docker Compose all chose it for readability and comments
- APIs and data interchange: JSON. Universal parser support and strict typing per the [JSON specification](https://www.ecma-international.org/publications-and-standards/standards/ecma-404/) make it the default for REST payloads and log streams
- Local development config: either works; teams already fluent in one tend to standardize on it
- Large generated documents: JSON. Machines write both equally well, but JSON parses faster at scale
When converting between the two mid-workflow, the JSON to YAML converter handles translation both directions without retyping structures.
Both benefit from schema validation
Whichever format carries your configuration, validation catches drift before runtime does. [JSON Schema](https://json-schema.org/) validates JSON natively, and YAML documents convert to JSON for the same validation pass since YAML is a superset. Our guide to what JSON Schema is covers building those validation contracts, and the JSON formatter catches syntax errors before validation even starts, and treating configuration as validated data rather than freeform text catches the indentation and coercion bugs this article describes before they reach production.
Common mistakes choosing between them
| Mistake | Consequence | Fix |
|---|---|---|
| Unquoted ambiguous YAML values | Silent boolean and number coercion | Quote version strings and codes |
| Comments in .json files | Parsers reject the file outright | Move rationale to schema descriptions |
| Hand-editing machine-generated JSON | Comma and brace errors | Regenerate or use a formatter tool |
| Deep indentation in shared YAML | Invisible space-count bugs across editors | Lint YAML in CI like code |
Choosing between YAML and JSON from here
Resolve every future yaml vs json question with audience: who writes this file, and who reads it? Humans editing by hand favor YAML's comments and structure; machines exchanging data favor JSON's strictness and speed. Most real systems use both, YAML where developers touch configuration and JSON everywhere data flows programmatically. Validate whichever you choose, because both formats happily carry mistakes into production when nobody checks.


