JSON and YAML are both data serialization formats used for configuration files, APIs, and data exchange. JSON is strict and machine-friendly. YAML is flexible and human-friendly — but that flexibility comes with gotchas. This guide covers the real differences and when each format is the right choice.
Quick Comparison
| Feature | JSON | YAML |
|---|---|---|
| Syntax | Braces and brackets | Indentation-based |
| Comments | Not allowed | # comment |
| Data types | String, number, boolean, null, array, object | Same + dates, timestamps, binary, anchors |
| Multiline strings | Escaped \n only | | (literal) and > (folded) |
| Trailing commas | Not allowed (strict) | No commas at all |
| File size | Larger (braces, quotes, commas) | Smaller (indentation only) |
| Parsing speed | Very fast (simple grammar) | Slower (complex grammar) |
| Multiple documents | Not supported | --- separator |
| Primary use | APIs, data exchange, package.json | Config files (K8s, Docker, CI/CD) |
The Same Data in Both Formats
{
"name": "my-api",
"version": "2.1.0",
"database": {
"host": "localhost",
"port": 5432,
"ssl": true,
"replicas": ["db-1.example.com", "db-2.example.com"]
},
"features": {
"cache": true,
"rateLimit": 1000,
"allowedOrigins": ["https://app.example.com", "https://admin.example.com"]
}
}name: my-api
version: "2.1.0" # Quote to prevent float interpretation
database:
host: localhost
port: 5432
ssl: true
replicas:
- db-1.example.com
- db-2.example.com
features:
cache: true
rateLimit: 1000
allowedOrigins:
- https://app.example.com
- https://admin.example.comWhere JSON Wins
- Unambiguous parsing. JSON has exactly one way to represent each value. No implicit type coercion, no whitespace sensitivity, no surprises.
- Universal native support. Every language has a built-in JSON parser. JavaScript's
JSON.parse()is the fastest serialization in the browser. - API standard. REST APIs, GraphQL responses, WebSocket messages, and JWT payloads all use JSON. It's the lingua franca of data exchange.
- Tooling. JSON Schema, JSON Path, JSON Patch, and JSON Pointer are mature specifications with broad ecosystem support.
Where YAML Wins
- Readability. No braces, no brackets, no quotes on keys. For config files that humans edit frequently, YAML is easier to scan.
- Comments. JSON has no comment syntax. YAML lets you annotate config with
# explanation. This alone drives many teams to YAML for configuration. - Multiline strings. YAML's
|(literal block) and>(folded block) make embedding SQL, shell scripts, or templates natural. - Anchors and aliases. YAML's
&anchorand*aliassystem lets you reuse config blocks without duplication — useful in CI/CD pipelines. - Multi-document files. A single YAML file can contain multiple documents separated by
---. Kubernetes uses this for deploying multiple resources from one file.
YAML's Gotchas
YAML's flexibility creates real foot-guns that have caused production incidents:
- The Norway problem.
NOis parsed as booleanfalsein YAML 1.1. Country codes, abbreviations, and environment names likeon/off/yes/noget silently converted. - Version numbers as floats.
version: 3.10becomes3.1because it's parsed as a floating-point number. Always quote version strings. - Indentation errors. Mixing tabs and spaces, or inconsistent indentation, produces parse errors or wrong nesting. JSON's braces make structure explicit regardless of whitespace.
- Security (yaml.load). YAML supports arbitrary object instantiation in some parsers. Python's
yaml.safe_load()exists becauseyaml.load()can execute code. JSON has no such risk.
# These are all boolean false in YAML 1.1:
country: NO # Parsed as false, not the string "NO"
flag: off # Parsed as false
answer: n # Parsed as false
# This loses precision:
version: 3.10 # Parsed as 3.1 (float)
# Fix: always quote ambiguous values
country: "NO"
version: "3.10"Managing YAML-heavy infrastructure?
DigitalOcean provides managed Kubernetes and simple cloud infrastructure. Validate your K8s manifests and Docker Compose files before deploying.
When to Use Each
| Use Case | Format | Why |
|---|---|---|
| REST APIs | JSON | Industry standard, native browser support |
| Kubernetes manifests | YAML | Comments, multi-doc, ecosystem convention |
| CI/CD pipelines | YAML | GitHub Actions, GitLab CI, CircleCI all use YAML |
| package.json / tsconfig | JSON | Tooling expects JSON, programmatic read/write |
| Docker Compose | YAML | Comments for documentation, readable service defs |
| Data interchange | JSON | Unambiguous, fast parsing, universal support |
The Verdict
Use JSON for data exchange — APIs, databases, and anything machines parse programmatically. Use YAML for configuration — files humans edit frequently where comments and readability matter. When in doubt, use JSON. It has fewer surprises, faster parsing, and universal support.
Try It Yourself
Convert between formats instantly with the JSON ↔ YAML Converter. Validate your JSON with the JSON Formatter & Validator, or check your YAML syntax with the YAML Validator & Formatter.