YAML vs JSON: Syntax, Types & When to Use Each
YAML vs JSON: Syntax, Types & When to Use Each
YAML and JSON are the two formats developers touch most often: JSON powers nearly every web API, while YAML dominates configuration — Kubernetes manifests, GitHub Actions workflows, Docker Compose files, and CI pipelines. The two are close cousins (every JSON document is technically valid YAML), yet they behave very differently in practice. If you need to move between them right now, try it with our Json ↔ Yaml Converter — it translates both directions instantly in your browser.
This guide compares YAML and JSON across syntax, comments, data types, parsing safety, and tooling, with real conversion output from the tool so you can see exactly what a round trip does to your data.
What Are YAML and JSON?
JSON (JavaScript Object Notation) is a strict, minimal data format built from
objects, arrays, strings, numbers, booleans, and null. Its grammar fits on a
postcard, which is why every language parses it natively and why machines love it.
YAML (YAML Ain't Markup Language) is a superset of JSON designed for human reading and writing. It replaces braces and brackets with indentation, drops most quotes, and adds comments, anchors, and multi-line strings — features aimed squarely at hand-edited configuration files.
The relationship matters: because YAML 1.2 is a superset, converting JSON → YAML always succeeds, while YAML → JSON can lose YAML-only features such as comments and anchors.
YAML vs JSON: A Side-by-Side Comparison
| Dimension | YAML | JSON |
|---|---|---|
| Syntax | Indentation-based | Braces, brackets, quotes |
| Comments | # supported | Not in the standard |
| Data types | Strings, numbers, booleans, null, dates* | String, number, boolean, null |
| Multi-line strings | Native block scalars | Escaped \n only |
| Anchors / references | Yes (&anchor, *ref) | No |
| Parsing speed | Slower (complex grammar) | Very fast |
| Ambiguity risk | Real (type inference) | Essentially none |
| Best at | Hand-edited config | Machine-to-machine data |
Rule of thumb: humans write YAML, machines exchange JSON. If a file is edited in a code editor by people, YAML's comments and clean syntax win. If it travels over a wire or gets generated by code, JSON's strictness wins.
Readability and Comments
The same config in both formats makes the difference obvious. JSON:
{
"name": "api-server",
"version": "2.1.0",
"debug": false,
"ports": [8080, 8443],
"database": { "host": "localhost", "pool": 10 }
}
YAML:
# Service definition — edited by the platform team
name: api-server
version: 2.1.0
debug: false
ports:
- 8080
- 8443
database:
host: localhost
pool: 10
YAML needs no braces, no trailing-comma anxiety, and — critically — supports comments. JSON's lack of comments is its single most complained-about limitation in config files, and it is why formats like JSONC exist. When a JSON config grows unreadable, run it through the JSON Formatter; when a YAML file drifts into inconsistent indentation, the YAML Formatter normalizes it.
The Type-Inference Trap
JSON types are explicit: "true" is a string, true is a boolean, and nothing is
ever guessed. YAML infers types from bare scalars, which creates famous foot-guns:
country: NO— under YAML 1.1 parsers,NObecomes booleanfalse(the "Norway problem"). YAML 1.2 parsers keep it as the string"NO".version: 2.10— parsed as the number 2.1, silently dropping the trailing zero. A semantic version must be quoted:version: "2.10".time: 08:30— some parsers read sexagesimal or truncate; quote anything that merely looks numeric.
When in doubt, quote your YAML strings. JSON never has this problem because quotes are mandatory.
Hands-on: Tested with the Tool
I verified the round-trip behavior with the Json ↔ Yaml Converter, using the indentation option set to 2 spaces.
Step 1 — JSON → YAML. I pasted the api-server JSON from above into the Json
pane. The Yaml pane updated live with:
name: api-server
version: 2.1.0
debug: false
ports:
- 8080
- 8443
database:
host: localhost
pool: 10
Reproducible observation: arrays become block-style - item lists, nesting becomes
2-space indentation, and version: 2.1.0 needs no quotes because 2.1.0 is not a
valid number — it stays a string on the way back.
Step 2 — YAML → JSON. I pasted that YAML back into the Yaml pane. The Json pane
produced exactly the original structure: "debug": false stayed a boolean,
"ports": [8080, 8443] stayed numbers, and "version": "2.1.0" stayed a string.
The round trip was lossless for this document.
Step 3 — the trap, confirmed. I then typed version: 2.10 into the Yaml pane.
The JSON output showed "version": 2.1 — the trailing zero was gone, confirming the
type-inference trap above. Typing country: NO returned "country": "NO" (kept as
a string), showing the converter follows YAML 1.2 semantics. Both checks ran
entirely in the browser with instant output.
Code Examples
JavaScript
import yaml from "js-yaml";
// YAML -> JS object -> JSON
const doc = yaml.load("debug: false\nports:\n - 8080\n - 8443\n");
console.log(JSON.stringify(doc)); // {"debug":false,"ports":[8080,8443]}
// JS object -> YAML
console.log(yaml.dump({ name: "api-server", pool: 10 }, { indent: 2 }));
// name: api-server
// pool: 10
Python
import json, yaml # pip install pyyaml
# YAML -> dict -> JSON
doc = yaml.safe_load("debug: false\nports:\n - 8080\n - 8443\n")
print(json.dumps(doc)) # {"debug": false, "ports": [8080, 8443]}
# dict -> YAML
print(yaml.safe_dump({"name": "api-server", "pool": 10}))
Note that PyYAML's safe_load still follows YAML 1.1 rules, so NO becomes False
in Python but stays "NO" in a YAML 1.2 parser — the same document can parse
differently across languages. That alone is a strong argument for JSON at system
boundaries.
Common Mistakes
- Using tabs in YAML — the spec forbids tabs for indentation; one tab character breaks the parse. A formatter catches this instantly.
- Unquoted version strings —
2.10,1e2, and0x1Aall get reinterpreted as numbers. Quote them. - Expecting comments to survive conversion — YAML → JSON drops every comment, because JSON has nowhere to put them.
- Deep nesting in YAML — beyond three or four levels, indentation errors become invisible to the eye. Validate the JSON equivalent against a schema with the JSON Schema Validator.
- Hand-writing JSON configs — missing commas and unquoted keys are the top JSON syntax errors. Draft in YAML, then convert.
Related Tools
- Convert both directions with the Json ↔ Yaml Converter.
- One-way batch conversion with the YAML to JSON Converter.
- Clean up indentation with the YAML Formatter.
- Pretty-print or minify with the JSON Formatter.
- Compare YAML against TOML configs via the YAML ↔ TOML Converter.
When to Use This Tool Instead of Code
Installing js-yaml or PyYAML makes sense inside an application that parses config
at runtime. It does not make sense for one-off jobs: translating a Docker Compose
file into JSON for an API call, checking what a suspicious bare scalar really parses
to, or converting a teammate's JSON snippet into a Kubernetes-style manifest. For
those, the Json ↔ Yaml Converter is faster than any script
— no dependencies, no environment, and nothing leaves your browser, which matters
when the config contains credentials or internal hostnames. Keep libraries for
production code paths; use the converter for inspection, debugging, and translation.