Skip to content

JSON vs YAML: differences and when to use each

How JSON and YAML compare on syntax, comments and data types, the YAML pitfalls to watch for, and which one fits APIs and which fits config files.

Guide5 min readUpdated

JSON and YAML describe the same kind of data: objects (maps), arrays (lists), strings, numbers, booleans and null. The difference is who they're written for. JSON is strict and easy for programs to produce and parse; YAML is lighter to read and write by hand, which is why config files favour it. Here's the same data in both:

{
  "service": "api",
  "replicas": 3,
  "ports": [80, 443],
  "env": {
    "DEBUG": false
  }
}
JSON
# Production settings
service: api
replicas: 3
ports:
  - 80
  - 443
env:
  DEBUG: false
YAML

The main differences

JSONYAML
StructureBraces, brackets and commasIndentation and dashes
CommentsNot allowedYes, with #
StringsAlways in double quotesQuotes usually optional
Multi-line textEscaped as \nBlock scalars (| and >)
Reuse (anchors)NoYes, with & and *
Spec sizeA few pages (RFC 8259)A long specification with several versions
ParsingBuilt into every language, fast and strictNeeds a library; behaviour differs between versions
Typical useAPIs, data exchange, storageConfig files: Kubernetes, Docker Compose, CI

YAML pitfalls to watch for

YAML's flexibility comes from guessing types for unquoted values, and that's where most YAML bugs come from. Many widely used parsers still follow YAML 1.1 rules. Python's PyYAML, for example, reads this file:

country: no
version: 1.10
zip: 012
on: yes
YAML input
{'country': False,
 'version': 1.1,
 'zip': 10,
 True: True}
What PyYAML 6 returns
  • The “Norway problem”: the country code no becomes false, and the key on becomes true. YAML 1.1 treats yes/no/on/off as booleans; YAML 1.2 parsers (such as js-yaml) don't.
  • Version numbers: 1.10 is read as the number 1.1.
  • Leading zeros: 012 is read as octal and becomes 10 in YAML 1.1 (12 in YAML 1.2), so ZIP codes and IDs change silently.
  • Indentation: one wrong space changes the structure or breaks the file, and tabs aren't allowed for indentation at all.

The fix is the same in every case: quote strings that could be misread, such as "no", "1.10" and "012". JSON avoids the whole problem because every string is quoted and there are only six value types.

When to use JSON

  • APIs and anything sent between programs: every language parses it natively and identically.
  • Data that machines write, such as logs, caches, exports and database documents.
  • When strictness matters more than convenience; a typo fails loudly instead of changing a value.
  • Browser code, where JSON is built in and YAML would need an extra library.

When to use YAML

  • Config files that people edit by hand and want to comment, like Kubernetes manifests, Docker Compose and CI pipelines.
  • Long, nested settings where braces and quotes add a lot of noise.
  • When a tool you use expects it; most of the DevOps ecosystem does.

Converting JSON to YAML

If you have JSON and a tool wants YAML, the JSON to YAML converter does it as you type. It quotes anything a YAML parser could misread (like no, 1.10 or dates), so the result means exactly what your JSON meant, and it runs in your browser so config secrets stay on your machine. Make sure the JSON is valid first with the JSON Validator.

Frequently asked questions

Is YAML a superset of JSON?

Since YAML 1.2, almost any JSON document is also valid YAML, so a YAML 1.2 parser can read it. The reverse isn't true: YAML features such as comments, anchors and unquoted strings aren't valid JSON.

Is JSON or YAML faster to parse?

JSON. Its grammar is much smaller and stricter, and every language has fast, built-in JSON parsers. That's one reason APIs almost always use JSON.

Can I convert between them without losing data?

JSON to YAML is lossless. YAML to JSON loses comments and anchors, and any YAML-only types (like dates or custom tags) have to be turned into strings or numbers.

Tools used in this guide

More guides