JSON vs YAML: Choosing the Right Format
JSON and YAML are both structured, human-readable formats, and they can represent most of the same data. Why does YAML exist at all? Because in the places where YAML shines, JSON is genuinely painful, and vice versa. This article walks through the tradeoffs and helps you pick the right tool for each job.
The shared ground
Both formats support strings, numbers, booleans, null, lists, and maps. YAML is technically a superset of JSON — any valid JSON document is also valid YAML. That means you can copy a JSON blob directly into a YAML file and it will parse.
Where YAML wins: human-edited config
YAML was designed to be written and read by humans. Three features make it great for config:
- Indentation-based structure — no braces or brackets cluttering the view.
- Comments — anything after
#is ignored, so you can annotate config inline. - Multi-line strings — pipe and greater-than indicators make embedding blocks of text easy.
That's why Kubernetes, GitHub Actions, Ansible, Docker Compose, and most CI systems use YAML instead of JSON. Operators edit these files frequently and need to leave notes for the next person.
Where JSON wins: machine data and APIs
- Unambiguous parsing — JSON has one way to write each value; YAML has many.
- Universal support — every language ships with a JSON parser; YAML requires an extra library.
- Fast to parse — JSON parsers are simpler and faster.
- No surprises — YAML's type inference is famously tricky (the "Norway problem", where
NObecomesfalse).
The Norway problem
YAML 1.1 treats yes, no, on, off, and a pile of other bareword tokens as booleans. So a list of country codes like [NO, DK, SE] silently becomes [false, "DK", "SE"]. YAML 1.2 tightened this up, but many parsers still default to 1.1. JSON has no such surprises — "NO" is always a string.
When to use which
Reach for YAML when:
- A human will edit the file frequently and needs comments.
- The file is a config for an ops tool that expects YAML (Kubernetes, GitHub Actions, etc.).
- You need to embed multi-line scripts or text blocks.
Reach for JSON when:
- The data is produced or consumed by a machine (APIs, storage, logs).
- Parsing speed matters.
- You want zero surprises from implicit type coercion.
- You're working in an environment without a YAML parser already loaded.
Using both together
Many teams author config in YAML and convert to JSON at build time for consumption by applications that only speak JSON, or vice versa. JSON Mint's JSON to YAML and YAML to JSON converters handle the conversion in your browser — paste one, get the other, no upload.
One common pattern is storing data in JSON (for machine use) and generating YAML just for manual editing via a small wrapper script. Another is the opposite: humans edit YAML, a build step emits JSON for the production system.
The short answer to "JSON or YAML?" is: ask who will be editing the file and what's going to read it. Machines prefer JSON. Humans with a keyboard prefer YAML.