YAML vs JSON: Differences, Pitfalls and When to Use Each
By the DataSwitch Pro team · · 3 min read
Quick answer
YAML and JSON can represent the same data — YAML 1.2 is even a superset of JSON — but YAML uses indentation instead of braces, allows comments and is easier for people to read and edit, while JSON is stricter, simpler to parse and less prone to ambiguity. Use YAML for configuration files that humans maintain (Kubernetes, Docker Compose, GitHub Actions) and JSON for APIs, data exchange and files written by programs.
Open a Kubernetes manifest and you see YAML; call almost any web API and you get JSON. Both describe nested maps, lists, strings, numbers and booleans. The choice between them is about who reads and writes the file, and how much ambiguity you can tolerate.
The same configuration in both
YAML
# web service
service:
name: api
replicas: 3
ports:
- 8080
- 8443
debug: false
JSON
{
"service": {
"name": "api",
"replicas": 3,
"ports": [8080, 8443],
"debug": false
}
}
Key differences
- Structure. YAML uses indentation (spaces only, never tabs); JSON uses braces, brackets and commas.
- Comments. YAML supports
# comments; JSON does not, which is the main reason JSON is unpopular for hand-edited config. - Quotes. Most YAML strings need no quotes. JSON requires double quotes around every string and key.
- Reuse. YAML has anchors and aliases (
&defaults,*defaults,<<:) to avoid repetition. JSON has none. - Multiple documents. One YAML file can hold several documents separated by
---. A JSON file is one value. - Multi-line text. YAML has block scalars (
|keeps line breaks,>folds them), handy for scripts and certificates. JSON needs\nescapes. - Parsing and safety. JSON parsers are small, fast and identical everywhere. YAML parsers are complex, differ between YAML 1.1 and 1.2, and some can create objects from tags — always use a “safe load” function for untrusted YAML.
YAML pitfalls to know
- Implicit types. Unquoted values are typed automatically. In YAML 1.1 parsers,
yes,no,onandoffbecome booleans — the famous “Norway problem”, where the country codeNOturns intofalse. - Numbers that are not numbers.
version: 1.10becomes the number 1.1, andzip: 02134may lose its zero or be read as octal in older parsers. Quote them. - Indentation errors silently change the structure: a key indented one level too far belongs to a different parent. See common YAML errors and how to fix them.
- Colons in values.
title: Note: read thisneeds quotes.
When to use YAML
- Configuration that people edit by hand: Docker Compose, Kubernetes, Ansible, GitHub Actions, OpenAPI specs.
- Files that benefit from comments explaining each setting.
- Content front matter in static site generators.
When to use JSON
- APIs, webhooks and data sent between programs.
- Files generated and read by software, where strictness prevents surprises.
- Browser code, where
JSON.parseis built in. - Large data sets — JSON parsing is significantly faster.
Converting between them
Because every JSON document is valid YAML 1.2, converting JSON to YAML is safe: a good converter quotes strings such as "yes" or "02134" so they keep their meaning. Converting YAML to JSON is also a quick way to check what a YAML file really means — the JSON shows exactly which values became numbers, booleans or nested objects. Use JSON to YAML and YAML to JSON. If you are weighing a third option, read TOML vs YAML vs JSON.
Try it on your own file. Free, private, and processed in your browser.
Frequently asked questions
Is JSON valid YAML?
Yes for YAML 1.2, with very rare edge cases. Most YAML parsers can read a JSON file directly.
Which is faster to parse?
JSON, by a wide margin, because its grammar is much simpler.
Are comments kept when converting YAML to JSON?
No. JSON has no comments, so they are dropped.
Should I use .yml or .yaml?
Both work. .yaml is the officially recommended extension; many tools such as Docker Compose accept either.