learn

JSON vs YAML

YAML 1.2 is a superset of JSON with comments and indentation. Compare syntax, types, the Norway problem, anchors and where each format fits best.

The short answer

JSON and YAML describe the same kind of data, objects, lists, strings, numbers, booleans and null, with different goals. JSON is strict and simple, so every language parses it the same way; it is the format of APIs. YAML is written for people: indentation instead of brackets, comments, multi-line strings and references, which is why it rules configuration files. Since YAML 1.2, every valid JSON document is also valid YAML, so converting from JSON to YAML never loses data; the other way loses comments and anchors.

The same data in both

{
  "name": "orders",
  "replicas": 3,
  "ports": [8080, 9090],
  "debug": false
}
# orders service
name: orders
replicas: 3
ports:
  - 8080
  - 9090
debug: false

Convert either way with JSON to YAML and YAML to JSON, and tidy each one with the JSON formatter or the YAML formatter.

Side by side

JSONYAML
Structurebraces and bracketsindentation
Commentsnoyes (#)
Stringsalways in double quotesquotes optional
Multi-line text\n escapesblock styles (literal block and folded block)
Reusenoanchors and aliases (&base, *base)
Parsingfast, few surprisesslower, implicit types
Typical useAPIs, data exchange, logsKubernetes, Docker Compose, CI pipelines

YAML pitfalls

Unquoted values are typed by their look, and the rules changed between YAML 1.1 and 1.2:

  • The Norway problem: in YAML 1.1, country: NO is the boolean false (also yes, no, on, off). YAML 1.2 parsers read it as the string "NO", but many tools still follow 1.1. Quote it: country: "NO".
  • Version numbers: version: 1.10 is the number 1.1. Write "1.10".
  • Leading zeros: zip: 01310 loses the zero (and is octal in YAML 1.1). Quote codes and identifiers.
  • Tabs are not allowed for indentation, and a wrong indent silently moves a key to another level.

JSON has its own traps: no trailing commas, no comments, and numbers above 2^53 lose precision in JavaScript. The JSON validator points to the exact line and column of a syntax error.

Which one to use

  • Data exchanged between programs, API payloads, anything generated by code: JSON.
  • Files edited by people, with comments explaining each setting: YAML (or TOML for simpler, flat configs).
  • Both in one project is normal: write configuration in YAML, let tools emit JSON. Other pairs, such as YAML to XML or .properties to YAML, are in the data converter.

FAQ

Is YAML a superset of JSON?

Yes, since YAML 1.2 (2009): a JSON document is valid YAML. Some parsers have small edge cases, but in practice JSON pastes into YAML files.

Does converting YAML to JSON lose anything?

Comments, anchors (expanded into copies) and formatting are lost; the data itself is kept.

Why is my YAML value false instead of "NO"?

Your parser follows YAML 1.1 rules, where NO is a boolean. Put the value in quotes.

Which is faster to parse?

JSON, by a wide margin, because its grammar is tiny. That matters for APIs, rarely for config files.