JSON vs YAML: Key Differences and When to Use Each Format
JSON and YAML both represent structured data, but they serve different masters. JSON was designed for machines; YAML was designed for humans. Choosing the wrong format can make your configs painful to maintain or your APIs hard to consume.
At a Glance: JSON vs YAML
| Feature | JSON | YAML |
|---|---|---|
| Comments | Not supported | Yes, using # |
| String quotes | Always required | Optional for simple strings |
| Whitespace | Insignificant | Significant (indentation = structure) |
| Trailing commas | Not allowed | N/A (no commas) |
| Boolean values | true / false only | true, false, yes, no, on, off |
| Parse speed | Very fast | Slower (5-10x) |
| Verbosity | More verbose | More concise |
| Parse errors | Usually obvious | Can be subtle |
| Binary data | Via Base64 | Native binary type |
| Primary use | APIs, serialization | Config files, CI/CD, k8s |
JSON in Practice
JSON (JavaScript Object Notation) was created by Douglas Crockford and is standardized as ECMA-404 and RFC 8259. It is deliberately minimal: six value types, strict quoting rules, no comments, no trailing commas. That strictness is a feature — JSON is trivial to parse reliably.
{
"service": "api-gateway",
"port": 8080,
"tls": true,
"allowedOrigins": ["https://app.example.com", "https://admin.example.com"],
"database": {
"host": "db.internal",
"port": 5432,
"name": "production"
}
}Every key must be a double-quoted string. Values can be strings, numbers, booleans (true/false), null, arrays, or nested objects. Nothing else.
YAML in Practice
YAML (YAML Ain't Markup Language) prioritizes human readability. It uses indentation for structure, makes quotes optional for most strings, and supports comments. The same config as above in YAML:
# API Gateway configuration
service: api-gateway
port: 8080
tls: true
allowedOrigins:
- https://app.example.com
- https://admin.example.com
database:
host: db.internal
port: 5432
name: productionNotice: no braces, no quotes around simple strings, no commas, and you can add explanatory comments. YAML is considerably easier to read and edit by hand.
Important Syntax Differences
1. Multiline strings
Embedding multiline text in JSON requires escape sequences (\n). YAML handles it natively with block scalar operators:
# YAML literal block (preserves newlines)
script: |
#!/bin/bash
echo "Building..."
npm install && npm run build
# YAML folded block (newlines become spaces)
description: >
This is a long description that wraps
across multiple lines in the source file.2. Anchors and aliases (YAML only)
YAML's killer feature for config files: define a value once and reuse it with &anchor and *alias:
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults # Merge the defaults anchor
endpoint: https://api.prod.example.com
staging:
<<: *defaults
endpoint: https://api.staging.example.com3. Implicit type coercion (YAML gotchas)
YAML infers types from unquoted values. This causes surprising behavior:
NO(ISO country code for Norway) parses as booleanfalsein YAML 1.1 — the infamous "Norway Problem"1.0is a float,1is an int — type matters for equality checks0777parses as octal 511 in some YAML 1.1 parsers- Tabs are never valid indentation in YAML — only spaces
Fix: quote values that should always be strings: country: "NO".
When to Choose JSON vs YAML
Choose JSON when:
- +Building a REST or GraphQL API
- +Storing data in localStorage or IndexedDB
- +Serializing data between services
- +Writing package.json or tsconfig.json
- +High-frequency parsing performance matters
- +The file is machine-generated, not hand-edited
Choose YAML when:
- +Writing GitHub Actions or GitLab CI configs
- +Creating Kubernetes or Helm manifests
- +Writing Docker Compose files
- +Configuring Ansible playbooks
- +The file needs to be readable and editable by humans
- +You need comments for documentation
Paste JSON or YAML and get an instant, correctly formatted conversion. Runs entirely in your browser — no data uploaded anywhere.
Frequently Asked Questions
Does YAML support comments?+
Yes. YAML supports single-line comments using the # character. JSON does not support comments at all — any comment syntax will cause a parse error.
Is JSON a subset of YAML?+
YAML 1.2 is designed so that every valid JSON document is also valid YAML. In practice this works for most cases, but edge cases exist with duplicate keys and certain number formats.
Why does YAML parse 'NO' as false?+
This is the 'Norway Problem' from YAML 1.1. The spec treats yes, no, on, off, true, and false (and uppercase variants) as booleans — so the country code 'NO' becomes boolean false. YAML 1.2 fixed this by only recognizing true and false as booleans. Quote the value as 'NO' to be safe.
Which is faster to parse: JSON or YAML?+
JSON is significantly faster. JSON parsers are simpler (no indentation tracking, no type inference, no anchors) and are often native code. YAML parsing is 5-10x slower in most benchmarks. Use JSON for high-frequency parsing or machine-generated data.
Can I convert JSON to YAML automatically?+
Yes — any valid JSON can be converted to YAML losslessly since all JSON types map cleanly to YAML. Use the noserver JSON to YAML Converter to do this in your browser.
What is the difference between YAML 1.1 and YAML 1.2?+
YAML 1.2 fixed implicit type coercion issues: booleans are only true/false, octal literals need the 0o prefix, JSON is an explicit subset. Many tools still ship YAML 1.1 parsers, so the Norway problem may still affect you.
When should I use TOML instead of YAML?+
TOML is great for simple, flat configuration files where readability matters and deep nesting is minimal. It avoids YAML's indentation sensitivity and implicit type coercion. Common in Rust (Cargo.toml), Python (pyproject.toml), and Hugo sites.