JSON vs. YAML: Syntax Differences, Pitfalls & Use Cases

JSON vs. YAML: Syntax Differences, Pitfalls & Use Cases

Compare JSON and YAML side by side, avoid YAML type traps like unquoted NO or version numbers and pick the right format for APIs and config files.

Updated 24 November 2026

JSON (JavaScript Object Notation) is a strict, minimal data interchange format built for machine-to-machine APIs and fast parsing, while YAML (YAML Ain't Markup Language) is a human-friendly superset of JSON built for configuration files that need comments, multiline blocks and clean readability. Choose JSON for APIs and storage, and YAML for hand-edited infrastructure configs.

Whenever you need to switch between the two formats cleanly, paste your payload into our free JSON / YAML Converter or validate strict syntax in the JSON Formatter. Below is a side-by-side comparison, the YAML type traps that cause production bugs and a safe conversion checklist.

JSON vs. YAML: side-by-side comparison

Since YAML 1.2 (2009), every valid JSON document under RFC 8259 is also valid YAML. In day-to-day work, however, the two formats optimize for very different priorities.

Feature JSON (RFC 8259) YAML (1.2)
Comments Not allowed Supported with #
Structure syntax Braces {}, brackets [], commas, double quotes Whitespace indentation (spaces only, never tabs)
String quoting All keys and strings must use double quotes Optional for plain scalars (source of type bugs)
Multiline strings Requires escaped \n inside one line Native literal (|) and folded (>) blocks
Parsing speed & safety Very fast, tiny spec, low ambiguity Slower, larger spec, parser version differences
Best suited for REST APIs, webhooks, browser storage, logs, package manifests Docker Compose, Kubernetes, GitHub Actions, CI pipelines

Four YAML pitfalls that break configs

Most teams love YAML for its clean visual layout until an implicit type conversion silently alters a value during deployment. Watch for these four traps, especially when tooling still uses a YAML 1.1 parser:

  1. The "Norway problem" (boolean coercion). In YAML 1.1, unquoted strings like NO, yes, on and off parse as booleans (false or true). A list of ISO country codes containing NO for Norway turns into false unless quoted as "NO".
  2. Version numbers becoming floats. Writing version: 1.10 without quotes parses as the floating-point number 1.1, dropping the trailing zero. Always quote version strings ("1.10") and postal codes ("02138").
  3. Sexagesimal time parsing and octals. Older YAML 1.1 parsers interpret unquoted values like 22:22 (such as Docker port mappings) as base-60 integers (1342) and leading-zero numbers like 0755 as octal. Quote ports ("8080:80") and file modes ("0755").
  4. Tabs vs. spaces in indentation. YAML forbids literal tab characters for indentation. A single tab pasted from a chat message throws a syntax error, and two extra spaces can silently move a key under the wrong parent object.

How to convert cleanly between JSON and YAML

  1. Validate the source first. If you start with JSON, run it through our JSON Formatter to catch trailing commas or single quotes before converting.
  2. Convert with our JSON / YAML Converter. Paste either format and verify the generated types right away.
  3. Quote ambiguous scalars explicitly. In YAML output destined for CI or Kubernetes, wrap version strings, country codes, port mappings and numeric IDs in quotes so both YAML 1.1 and YAML 1.2 parsers read them as strings.
  4. Remember comments disappear when going YAML to JSON. JSON has no comment syntax, so back up important inline notes before exporting to JSON.
  5. Diff before deploying. Compare the old and new config files side by side with our Text Diff Viewer to make sure only the intended keys changed.

Working in PHP or Laravel? You can also turn JSON payloads directly into clean PHP short array syntax with our JSON to PHP Array tool. For deeper syntax references, see our guides to formatting and validating JSON and converting JSON and YAML online.

Frequently asked questions

Is JSON valid YAML?

Yes. Since the YAML 1.2 specification, JSON is a strict subset of YAML, which means any valid JSON file can be parsed by a compliant YAML 1.2 parser without modification. The reverse is not true: YAML features like comments, anchors and unquoted keys are invalid in JSON.

Why does YAML turn NO into false?

YAML 1.1 defined unquoted yes, no, on and off (in any letter case) as boolean literals. While YAML 1.2 narrowed booleans to true and false, many widely used libraries still default to YAML 1.1 rules, so quoting "NO" prevents bugs across every parser.

Should REST APIs use JSON or YAML?

Always use JSON for REST APIs and webhooks. JSON parsers are built into every browser and standard library, run faster, avoid implicit type surprises and have zero indentation sensitivity over the wire.

Can I add comments to a JSON file?

Not in standard RFC 8259 JSON. Some developer tools accept JSONC (JSON with comments) for editor configs like tsconfig.json, but standard APIs and JSON.parse() reject comments. If humans need to annotate a config file heavily, YAML or TOML is a cleaner choice.

What happens to YAML anchors and aliases when converted to JSON?

JSON has no anchor or alias syntax, so converting YAML with anchors (&) and aliases (*) to JSON expands the referenced values into repeated concrete objects or fields in the JSON output.

Ready to use this tool?

Try the JSON / YAML Converter now and simplify your workflow immediately.

Great Online Apps
Toggle sidebar