EasyDeveloper

JSON

JSON vs YAML: Which Format Should You Use, and How Do You Convert?

Published 2026-08-15 · 8 min read

TL;DR: Use JSON for data crossing system boundaries and YAML for configuration humans write; a converter switches between them, and each format has footguns the other does not.

JSON and YAML both serialize data as text, and both are everywhere: JSON in APIs and storage, YAML in Kubernetes, CI pipelines and Ansible. The choice is rarely about capability - a converter moves data between them losslessly in most cases - and mostly about who writes and reads the file. This guide compares them and shows where each wins. JSON follows the JSON specification; YAML is standardized separately.

How are JSON and YAML different?

AspectJSONYAML
SyntaxBraces, brackets, double-quoted stringsIndentation, line-based
CommentsNoneSupported with #
Key styleKeys must be quoted stringsKeys usually unquoted
Multiline stringsEscape sequencesBlock scalars with | and >
Data typesString, number, boolean, null, object, arrayAdds dates, anchors and aliases
ParsersEvery languageMany, with behavioral differences

When should you pick JSON?

Pick JSON when data crosses a system boundary. Every language has a strict JSON parser that behaves the same way, the grammar is small enough to be implemented correctly, and the format is unambiguous. That makes it the right choice for API responses, database records, event payloads and files that a program reads and writes. The JSON formatter keeps such files readable.

When should you pick YAML?

Pick YAML when a human writes the file. Config files, CI definitions, Kubernetes manifests and Ansible playbooks are authored by people, and YAML reads closer to prose: no braces, comments for context, and multiline strings that do not need escape sequences.

# docker-compose.yml
services:
  web:
    image: nginx:1.27
    ports:
      - "8080:80"

What are the YAML footguns?

YAML 1.1 has behaviors that surprise people who assume it matches YAML 1.2 or JSON. The classic ones: tabs are not allowed for indentation, yes and no parse as booleans in many 1.1 parsers, and on and off too. A value like version: 1.0 can become the number 1 in some parsers. The general fix is to quote values that must stay strings:

port: "8080"       # quoted, stays a string
enabled: true      # boolean in both dialects

If your file works on your machine but not in CI, the first thing to check is which YAML version each parser implements. The JSON to YAML converter produces clean YAML that avoids the ambiguity by quoting strings that need it.

Do JSON and YAML parse to the same values?

Not always. The two formats agree on most plain data, but they disagree on a few value types, and the gap has bitten many a config file. The classic case is the string yes. In JSON, yes is a string. In YAML 1.1, the older rule set many tools still bundle, yes and on and no and off parse as booleans, so a config key you meant as text becomes true or false silently.

# YAML 1.1
retry: yes        # becomes the boolean true
count: 1e3        # becomes the number 1000

# same intent in JSON
"retry": "yes"    # stays a string
"count": 1e3      # stays 1000

YAML 1.2, the current standard, removes the yes and on and off mapping, but many YAML libraries still default to the 1.1 rules. If you round-trip data between YAML and JSON, quote anything that looks like a boolean keyword and pin your parser to 1.2 where possible.

How do you convert between JSON and YAML?

The JSON to YAML tool takes a JSON document and produces the equivalent YAML; the YAML to JSON tool goes the other way. In code, Python has yaml.safe_load and yaml.safe_dump, and JavaScript has the js-yaml package. Round-tripping works because the two formats describe the same data types.

One caveat on conversion: YAML dates and anchors are YAML-only and have no JSON equivalent. If a YAML file uses anchors to repeat blocks, the JSON output duplicates the content.

How strict are JSON and YAML parsers?

Strictness is the biggest practical difference. A JSON parser must reject trailing commas, comments and single quotes; there is no valid variation. A YAML parser has to accept much looser input, which is why the same file can parse in one tool and crash in another. That flexibility helps humans write config by hand, and it hurts when two systems must agree on what a value means.

For machine-to-machine data the strictness of JSON is usually the point. The parser fails loudly at the exact byte that breaks the grammar, which makes bugs easier to trace than a YAML file that parses but produces the wrong type.

Which one is right for your file?

  1. If a program writes the file and another program reads it, use JSON.
  2. If a human writes the file by hand, use YAML.
  3. If the file has comments, use YAML, because JSON has no comment syntax.
  4. If the file must parse identically in every language, use JSON.

Frequently Asked Questions

Is YAML a superset of JSON?

YAML 1.2 aims to be, and valid JSON is valid YAML in most 1.2 parsers. YAML 1.1, still the default in several tools, is not. Check the parser version before assuming compatibility.

Why does my YAML value become a number?

YAML 1.1 interprets yes, no, on, off and 1.0 as non-string types in some parsers. Quote the value to force a string.

Can I use tabs in YAML indentation?

No. YAML requires spaces for indentation; tabs cause parse errors in strict parsers.

Does conversion lose comments?

Yes. JSON has no comments, so converting YAML to JSON drops them, and there are no JSON comments to convert back.