JSON vs YAML
JSON and YAML describe the same kind of data (objects, lists, strings, numbers, booleans, and null) but are optimized for different readers. JSON uses braces, brackets, and quotes so that machines can parse it quickly and unambiguously. YAML uses indentation and minimal punctuation so that people can read and edit it comfortably. In short: use JSON for data that programs exchange, and YAML for configuration that humans write.
Side-by-side comparison
| Aspect | JSON | YAML |
|---|---|---|
| Designed for | Data interchange between programs | Human-edited configuration |
| Structure | Braces { } and brackets [ ] | Indentation (spaces only, no tabs) |
| Strings | Always double-quoted | Quotes usually optional |
| Comments | Not allowed | Allowed (#) |
| Data types | String, number, boolean, null, array, object | Same, plus dates and custom tags; types inferred from the value |
| Reuse | None | Anchors &, aliases *, merge keys << |
| Multiple documents | One per file | Several per file, separated by --- |
| Spec size | A few pages (RFC 8259) | Dozens of pages (YAML 1.2) |
| Parsing speed | Fast; built into every runtime | Slower; needs a library |
| Typical use | REST APIs, browser data, logs, storage | Kubernetes, Docker Compose, GitHub Actions, Ansible, CI config |
The same data in JSON and YAML
A small service config, first as JSON:
{
"service": "api",
"replicas": 3,
"debug": false,
"ports": [8080, 8443],
"database": {
"host": "db.internal",
"user": "app"
}
}And the same thing as YAML:
# API service settings service: api replicas: 3 debug: false ports: - 8080 - 8443 database: host: db.internal user: app
The YAML version drops the braces, commas, and quotes, and carries a comment. The structure lives entirely in the indentation, which is why a single misplaced space can change its meaning. Convert between the two with YAML to JSON and JSON to YAML.
Key differences
- Readability: YAML is easier to scan and edit by hand; JSON is noisier but completely explicit.
- Comments: YAML supports
#comments. JSON has none, which is the most common reason config moves to YAML (see can JSON have comments?). - Whitespace: in JSON, indentation is cosmetic. In YAML it is the structure, and tabs are not allowed.
- Types: JSON types are visible in the syntax (
"3"is a string,3is a number). YAML infers types from the bare value, which is convenient and occasionally surprising (see the gotchas below). - Reuse: YAML anchors let you define a block once and reference it elsewhere; JSON repeats it.
YAML gotchas JSON doesn't have
Because YAML guesses types from unquoted values, some inputs don't mean what they look like, especially with parsers that follow the older YAML 1.1 rules (still the default in many libraries):
- The Norway problem:
country: NObecomes the booleanfalseunder YAML 1.1, as doyes,on, andoffvariants. - Version numbers:
version: 1.10is the float1.1in every YAML version. Quote it:version: "1.10". - Leading zeros:
zip: 01234becomes the number1234(the zero is lost), or the octal value668under YAML 1.1. Quote codes and IDs. - Indentation errors: one wrong space moves a key to a different parent without any syntax error.
The fix for all of them is the same: quote values that must stay strings, and prefer a YAML 1.2 parser. Our YAML to JSON converter uses YAML 1.2 rules, so it is a quick way to see exactly how a file will be interpreted.
Security: parse YAML safely
JSON parsers only ever produce plain data. Some YAML libraries can do more: YAML tags can ask the loader to construct arbitrary objects, and in Python, yaml.load with an unsafe loader has led to remote code execution bugs. Always use the safe API for untrusted input, such as yaml.safe_load in PyYAML. For data arriving from outside your system (API requests, uploads), JSON is the safer default.
When to use JSON
- REST APIs, webhooks, and anything sent to or from a browser.
- Data written and read by programs: caches, logs, exports, storage.
- Files where strictness matters more than readability, like
package.jsonand lock files. - High-volume or performance-sensitive parsing.
When to use YAML
- Configuration that people edit by hand and want to comment.
- Ecosystems that standardize on it: Kubernetes manifests, Helm charts, Docker Compose, GitHub Actions, GitLab CI, Ansible playbooks.
- Long config with repeated blocks that benefit from anchors.
OpenAPI is a good example of both: the spec allows either format, and teams usually write it in YAML and serve it to tools as JSON.
Working with JSON and YAML
- YAML to JSON: see exactly how a YAML file is parsed, anchors expanded.
- JSON to YAML: turn JSON into readable YAML config.
- JSON vs XML: how JSON compares with the other classic format.
- JSON Formatter: pretty-print and validate JSON.
- TOON vs JSON: a newer format that borrows YAML-style indentation to save LLM tokens.
Frequently asked questions
What is the main difference between JSON and YAML?
JSON is a strict data format built from braces, brackets, and quoted strings, designed for machines to exchange data. YAML expresses the same data with indentation and minimal punctuation, adds comments, anchors, and multiple documents per file, and is designed for humans to read and edit.
Is YAML a superset of JSON?
Mostly, yes. YAML 1.2 was aligned with JSON so that practically any JSON document is also valid YAML: you can paste JSON into a YAML file and a YAML 1.2 parser will read it. The reverse is not true; most YAML (indentation, comments, anchors) is not valid JSON.
Is YAML better than JSON for configuration files?
For files people edit by hand, usually yes: YAML allows comments, needs fewer quotes and brackets, and reads cleanly. That is why Kubernetes, Docker Compose, GitHub Actions, and Ansible use it. For config generated or read by programs, JSON is simpler and safer to parse.
Is JSON faster than YAML?
Yes. JSON has a tiny grammar and highly optimized parsers (JSON.parse is built into every browser and runtime). YAML parsing is considerably more complex, so for large or high-volume data JSON is faster and uses less memory.
Can I use JSON instead of YAML in Kubernetes?
Yes. The Kubernetes API speaks JSON natively, and kubectl apply -f accepts .json manifest files. YAML is just the more common, more readable way to write manifests by hand.
Why does YAML turn NO into false?
YAML 1.1 treats words like yes, no, on, and off as booleans, so a country code NO (Norway) became false. YAML 1.2 limits booleans to true and false, but many parsers still default to 1.1 rules. Quote values that must stay strings, or use a 1.2-compliant parser.