JSON vs YAML: A Practical Comparison

JSON and YAML describe the same kinds of structured data, but they trade off differently between machine-friendliness and human readability. Here is how they compare and when to reach for each.

The Same Data, Two Notations

JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) both model the same building blocks: mappings (objects), sequences (arrays), and scalars (strings, numbers, booleans, null). In fact, YAML is a superset of JSON — most valid JSON is also valid YAML. The difference is style: JSON leans on explicit punctuation, while YAML leans on indentation and whitespace to convey structure.

Here is the same record written both ways:

// JSON
{
  "name": "Formatly",
  "tools": ["json", "yaml", "csv"],
  "private": true,
  "limits": null
}
// YAML
name: Formatly
tools:
  - json
  - yaml
  - csv
private: true
limits: null

Both encode an object with a string, an array of three strings, a boolean, and a null. YAML drops the braces, brackets, and most quotes; JSON keeps them.

Syntax Differences That Matter

  • Structure: JSON uses {} for objects and [] for arrays. YAML uses indentation for nesting and a leading - for list items. (YAML also supports inline "flow" style — tools: [json, yaml, csv] — which looks JSON-like.)
  • Quotes: JSON requires double quotes around every key and every string value. YAML usually lets you omit quotes, only needing them for ambiguous values.
  • Comments: YAML supports comments with #; JSON has no comment syntax at all. This alone makes YAML attractive for hand-edited config files.
  • Trailing commas: Standard JSON forbids a trailing comma after the last element. YAML has no separating commas in block style, so the problem disappears.
  • Verbosity: JSON is more compact on a single line and tends to be smaller over the wire; YAML is more vertical and easier to scan in a diff.

Features YAML Has and JSON Lacks

Anchors and aliases let you define a block once and reuse it, avoiding repetition:

defaults: &defaults
  retries: 3
  timeout: 30

staging:
  <<: *defaults
  url: https://staging.example.com

Here &defaults names a node, *defaults references it, and <<: merges it. JSON has no equivalent — you would copy the values by hand.

Multi-line strings are far cleaner in YAML. The literal block scalar | preserves newlines, and the folded scalar > joins wrapped lines with spaces:

script: |
  echo "line one"
  echo "line two"

In JSON the same value would be one long string with explicit \n escapes: "echo \"line one\"\necho \"line two\"\n".

Gotchas: The Famous "Norway Problem"

YAML's friendly, quote-optional scalars are also its biggest footgun. Older YAML parsers (the YAML 1.1 spec) interpret a long list of bare words as booleans: yes, no, true, false, on, and off. The classic example is a list of country codes:

countries:
  - NO   # Norway → parses as the boolean false!
  - SE
  - GB

Because NO can be read as "no", it may silently become false. The fix is to quote it: "NO". Related traps include version numbers like 1.20 losing the trailing zero (it becomes the number 1.2), and values like 10:30 being read as a sexagesimal number rather than a time string. The newer YAML 1.2 core schema removes the yes/no/on/off aliases, but many real-world tools still run on 1.1 behavior, so quote anything that could be misread.

The other recurring YAML pain is indentation: tabs are forbidden, and a single misaligned space changes the structure or breaks parsing. JSON avoids both problems because its brackets make boundaries explicit and its type rules are strict — a string is always quoted, so "NO" is never a boolean.

When to Use Each

  • Reach for JSON for machine-to-machine interchange: REST and GraphQL API payloads, web tokens, package manifests (package.json), and anything parsed by code at high volume. Its strictness and ubiquity make it the safe default for data on the wire.
  • Reach for YAML for human-authored configuration where comments and readability pay off: Kubernetes manifests, GitHub Actions and other CI pipelines, Docker Compose, Ansible playbooks, and app config files.

Converting Between Them with Formatly

You rarely have to rewrite by hand. Formatly's JSON to YAML Converter turns either format into the other. Paste your data, set the Direction toggle to JSON → YAML or YAML → JSON, and read the result; the Swap button flips direction and feeds the output back as new input. JSON output is pretty-printed with two-space indentation and syntax highlighting, and invalid input returns a precise parser error so you can fix it.

Because the converter runs entirely in your browser using JavaScript, your data is never uploaded, transmitted, or stored on any server — it works offline once the page has loaded, which matters when your config holds secrets, hostnames, or other sensitive values.

FAQ

Is YAML really a superset of JSON?

Yes. The YAML 1.2 specification was aligned so that valid JSON is also valid YAML, meaning a YAML parser can read JSON directly. The reverse is not true — YAML features like comments, anchors, and unquoted strings are not legal JSON, so converting YAML to JSON may drop comments and expand aliases into plain values.

Why does JSON not allow comments?

JSON was deliberately kept minimal as a data-interchange format, and comments were left out so they could not be misused to carry parsing directives. If you need annotated config, YAML (which uses # for comments) is usually the better choice. To strip comments while moving config into JSON, convert with Formatly's JSON to YAML Converter — the conversion runs locally in your browser, so nothing is uploaded.

What is the Norway problem in YAML?

It is a nickname for YAML's habit of reading bare words like no, yes, on, and off as booleans. The country code NO (Norway) can silently become false. Quote such values — "NO" — to force them to stay strings. YAML 1.2's core schema removes these aliases, but many tools still behave like YAML 1.1.

Which is faster or smaller, JSON or YAML?

JSON is generally more compact and faster to parse, which is why it dominates API traffic. YAML is more verbose and its richer rules make parsing slower, but those features buy readability and comments that are valuable in human-edited config files. Choose based on whether a machine or a person is the main reader.

Will converting between JSON and YAML change my data?

The structure and values are preserved — keys, arrays, nesting, numbers, booleans, and null all carry across. What can change is presentation: YAML comments are lost when going to JSON, anchors/aliases are expanded into their resolved values, and key ordering or string quoting may be normalized. Formatly's converter keeps YAML output unwrapped so long strings stay intact.

Is the JSON to YAML conversion private?

Yes. Formatly's converter is 100% client-side: it runs in your browser with JavaScript and never sends your input to a server. After the page loads it works offline, so even configuration containing secrets or internal hostnames stays on your own device.