JSON and YAML both describe structured data, the same objects, arrays, strings and numbers, but in very different styles. The short version: use JSON for data that machines exchange (APIs, message payloads), and use YAML for files that people write and read by hand (configuration). Here’s why.
The same data, side by side
An identical configuration in each format:
# JSON
{
"service": "api",
"port": 8080,
"features": ["auth", "logging"],
"debug": false
}
# YAML
service: api
port: 8080
features:
- auth
- logging
debug: falseJSON marks structure with {}, [], commas and quotes. YAML marks it with indentation and dashes, and drops most of the punctuation. Neither is “better” in the abstract; they optimise for different readers.
The differences that actually matter
- Comments. YAML has them (
#); JSON does not. For a config file people maintain, this alone often decides it. - Readability. YAML’s lack of braces and quotes makes long files easier to scan. JSON’s punctuation makes structure unambiguous at the cost of noise.
- Strictness. JSON has one obvious way to write things. YAML is flexible, sometimes too flexible (see the gotchas below).
- Whitespace. In JSON, indentation is cosmetic. In YAML it is structural: a wrong indent changes the meaning or breaks the parse.
- Support. Every language parses JSON out of the box. YAML usually needs a library.
- Speed. JSON parses faster, which matters for high-volume machine traffic and not at all for a config file loaded once at startup.
YAML’s famous gotchas
YAML’s convenience has sharp edges. The classic is the “Norway problem”: unquoted no can be read as the boolean false, so a list of country codes containing NO can surprise you. Numbers with leading zeros, version strings like 1.10, and times can all be coerced into the wrong type. When in doubt, quote the value.
A rule of thumb
Reach for JSON when a program is producing or consuming the data and no human needs to edit it, such as API responses, tokens and logs. Reach for YAML when a human is the primary author and comments and readability help, such as CI pipelines, Kubernetes manifests and app config. And because valid JSON is also valid YAML, you can always start in one and convert.
Need to move between the two? Our free YAML to JSON converter (and the JSON to YAML direction) translates instantly in your browser, so you can keep the format each job calls for.