JSON and YAML describe the same kind of data: objects (maps), arrays (lists), strings, numbers, booleans and null. The difference is who they're written for. JSON is strict and easy for programs to produce and parse; YAML is lighter to read and write by hand, which is why config files favour it. Here's the same data in both:
{
"service": "api",
"replicas": 3,
"ports": [80, 443],
"env": {
"DEBUG": false
}
}# Production settings
service: api
replicas: 3
ports:
- 80
- 443
env:
DEBUG: falseThe main differences
| JSON | YAML | |
|---|---|---|
| Structure | Braces, brackets and commas | Indentation and dashes |
| Comments | Not allowed | Yes, with # |
| Strings | Always in double quotes | Quotes usually optional |
| Multi-line text | Escaped as \n | Block scalars (| and >) |
| Reuse (anchors) | No | Yes, with & and * |
| Spec size | A few pages (RFC 8259) | A long specification with several versions |
| Parsing | Built into every language, fast and strict | Needs a library; behaviour differs between versions |
| Typical use | APIs, data exchange, storage | Config files: Kubernetes, Docker Compose, CI |
YAML pitfalls to watch for
YAML's flexibility comes from guessing types for unquoted values, and that's where most YAML bugs come from. Many widely used parsers still follow YAML 1.1 rules. Python's PyYAML, for example, reads this file:
country: no
version: 1.10
zip: 012
on: yes{'country': False,
'version': 1.1,
'zip': 10,
True: True}- The “Norway problem”: the country code
nobecomesfalse, and the keyonbecomestrue. YAML 1.1 treats yes/no/on/off as booleans; YAML 1.2 parsers (such as js-yaml) don't. - Version numbers:
1.10is read as the number 1.1. - Leading zeros:
012is read as octal and becomes 10 in YAML 1.1 (12 in YAML 1.2), so ZIP codes and IDs change silently. - Indentation: one wrong space changes the structure or breaks the file, and tabs aren't allowed for indentation at all.
The fix is the same in every case: quote strings that could be misread, such as "no", "1.10" and "012". JSON avoids the whole problem because every string is quoted and there are only six value types.
When to use JSON
- APIs and anything sent between programs: every language parses it natively and identically.
- Data that machines write, such as logs, caches, exports and database documents.
- When strictness matters more than convenience; a typo fails loudly instead of changing a value.
- Browser code, where JSON is built in and YAML would need an extra library.
When to use YAML
- Config files that people edit by hand and want to comment, like Kubernetes manifests, Docker Compose and CI pipelines.
- Long, nested settings where braces and quotes add a lot of noise.
- When a tool you use expects it; most of the DevOps ecosystem does.
Converting JSON to YAML
If you have JSON and a tool wants YAML, the JSON to YAML converter does it as you type. It quotes anything a YAML parser could misread (like no, 1.10 or dates), so the result means exactly what your JSON meant, and it runs in your browser so config secrets stay on your machine. Make sure the JSON is valid first with the JSON Validator.