YAML and JSON represent the same underlying data model — nested key-value structures, arrays, strings, numbers, booleans, and null. But they look radically different and are optimized for different contexts. YAML prioritizes human readability at the cost of strictness. JSON prioritizes machine readability and broad ecosystem support. Choosing the wrong format for a given task creates unnecessary friction for development teams.
A Side-by-Side Syntax Comparison
Consider the same data structure in both formats:
YAML version:
name: Alice
age: 30
skills:
- JavaScript
- Python
- Go
address:
city: San Francisco
state: CA
JSON version:
{
"name": "Alice",
"age": 30,
"skills": [
"JavaScript",
"Python",
"Go"
],
"address": {
"city": "San Francisco",
"state": "CA"
}
}
YAML is immediately more readable. It omits braces and quotes, uses indentation to define structure, and reads almost like English. JSON is explicit — every syntax element is visible, which makes parsing straightforward.
YAML's Unique Features
Comments
YAML supports comments (lines starting with #). JSON does not.
# User configuration
name: Alice # First and last name
age: 30 # Years old
Multiline Strings
YAML has two ways to represent multiline text:
Literal block (|) — Preserves line breaks:
description: |
This is a long description.
It spans multiple lines.
Newlines are preserved.
Folded block (>) — Converts newlines to spaces:
summary: >
This is a long summary
that will be folded into
a single line.
Anchors and Aliases
YAML lets you define a value once and reference it multiple times using anchors (&) and aliases (*):
default_settings: &defaults
timeout: 30
retries: 3
production:
<<: *defaults # Inherit all defaults
timeout: 60 # Override timeout
staging:
<<: *defaults
Multiple Documents
A single YAML file can contain multiple documents separated by ---. JSON doesn't support
this.
JSON's Strengths
Strict grammar with no ambiguity: JSON parsing is unambiguous. A valid JSON string always means the same thing to every parser.
Native JavaScript support: The syntax JSON.parse() and
JSON.stringify() are built-in to every JavaScript runtime.
Universal language support: Every programming language has a JSON parser. It's a language-agnostic standard.
No whitespace significance: Indentation is optional in JSON (unlike YAML, where incorrect indentation changes meaning). This makes JSON robust to transmission and editing tools.
Faster parsing in most runtimes: JSON parsers are simpler and more optimized because the grammar is stricter.
Easy to generate programmatically: Since JSON is explicit, programs can generate it without worrying about indentation or escaping nuances.
When to Use YAML
- Configuration files intended to be read and edited by humans frequently (Docker Compose, Kubernetes manifests, GitHub Actions, Ansible, Helm Charts)
- Infrastructure-as-code: DevOps and SRE teams use YAML for Kubernetes, Terraform, and infrastructure definitions
- CI/CD pipelines: GitHub Actions, GitLab CI, Jenkins use YAML for workflow definitions
- When comments are valuable: YAML comments help humans understand intent and context
- Human-edited files: If a file is primarily edited by people (not programs), YAML is more ergonomic
When to Use JSON
- API payloads and responses: REST APIs exchange JSON by convention
- Data stored in databases: NoSQL databases (MongoDB, CouchDB) use JSON-like structures
- Configuration generated by programs: If software generates the file, JSON is simpler and less error-prone
- Web-facing applications: Browsers natively understand JSON
- Package manifests: package.json, composer.json, Cargo.toml use JSON or JSON-like syntax
- When parsing strictness matters: If you need guaranteed parse success on any valid input, JSON is safer
The Norway Problem
YAML has an infamous gotcha called the "Norway problem." The country code for Norway is NO.
In YAML, NO is interpreted as a boolean false:
countries:
- YES
- NO
- MAYBE
Parses as:
countries:
- true
- false
- "MAYBE"
YAML's implicit type coercion causes confusion. Quote values when they might be misinterpreted:
countries:
- "YES"
- "NO"
- "MAYBE"
Performance Considerations
JSON parsing is generally faster than YAML parsing because JSON's grammar is simpler and more strictly defined. For systems processing millions of records per second, JSON's speed advantage matters.
YAML's flexibility (comments, anchors, implicit types) adds parsing complexity. For very large data files parsed at runtime, JSON is the practical choice. However, for configuration files (typically kilobytes, parsed once at startup), the performance difference is negligible.
Conversion Between YAML and JSON
YAML and JSON are semantically equivalent in most cases. You can convert between them, but:
- YAML comments are lost when converting to JSON
- YAML anchors and aliases are expanded to their values
- YAML implicit types require careful handling (e.g.,
NOas boolean vs. string) - Multiline strings may lose formatting nuances
Use the DevTools YAML-to-JSON converter for quick conversions. The tool handles all these edge cases automatically.
Practical Recommendation
Use YAML for configuration files edited by humans. Kubernetes YAML, Docker Compose, GitHub Actions workflows — these are human-maintained. YAML's readability is invaluable.
Use JSON for data exchanged between systems. REST API payloads, database records, file formats — JSON's universality and strictness matter here.
Don't overthink it. If your team has a convention (e.g., all configs are YAML), follow it. Consistency matters more than minor syntax preferences.