Software

YAML vs. JSON: The Same Config Parsed Two Ways

YAML and JSON can hold the same data but parse differently. One config run through PyYAML and js-yaml shows where YAML surprises you, and when to use each.

JSON is a strict data format: quoted keys, braces, no comments, one obvious way to parse it. YAML is a more readable superset-style format with indentation, comments and unquoted values, but its implicit typing means the same file can parse differently depending on the parser and YAML version. Use JSON for data exchanged between programs; use YAML for hand-edited configuration, and quote any value that could be misread.

One config, three parsers

Here is a small service config written in YAML:

service: checkout
replicas: 3
version: 1.10
country: NO
debug: off
enabled: yes
port: 0755
timeout: 30s
ports:
  - 8080
  - 8443

We parsed it with PyYAML 6.0.3 (Python’s most common YAML library) and js-yaml 4.1.0 (JavaScript’s), and printed the results as JSON:

KeyYAML textPyYAML 6.0.3js-yaml 4.1.0
version1.101.11.1
countryNOfalse"NO"
debugofffalse"off"
enabledyestrue"yes"
port0755493755
timeout30s"30s""30s"

Three different behaviours show up:

  • Both parsers turned 1.10 into the number 1.1. An unquoted version string is a float in every YAML version.
  • PyYAML follows YAML 1.1, where yes, no, on and off are booleans; so Norway’s country code NO became false. js-yaml follows the YAML 1.2 core schema, where only true and false are booleans.
  • PyYAML read 0755 as octal (7 × 64 + 5 × 8 + 5 = 493). js-yaml read it as decimal 755.

The equivalent JSON, with quotes where we meant strings, parsed exactly as written with JSON.parse: "version":"1.10", "country":"NO", "port":755. JSON has no implicit typing to get wrong.

Try your own YAML. The parser below is js-yaml 4.1.0, and it warns about values a YAML 1.1 parser would read differently:

Try itEdit the YAML to see the parsed JSON and any parser-dependent values.
{
  "service": "checkout",
  "replicas": 3,
  "version": 1.1,
  "country": "NO",
  "enabled": "yes",
  "ports": [
    8080,
    8443
  ]
}

Watch: 1.10, NO, yes. A YAML 1.1 parser (e.g. PyYAML) reads yes/no/on/off as booleans, and any parser reads 1.10 as the number 1.1. Quote values you mean as strings.

Side-by-side comparison

JSONYAML
Commentsnoyes, #
Quotesrequired for strings and keysoptional (implicit typing)
Structurebraces and bracketsindentation (spaces only)
Trailing commasinvalidnot applicable
Multiple documents per filenoyes, separated by ---
Referencesnoanchors and aliases (&a, *a)
Parsing behaviouruniform across parsersvaries by version and library
Speed and toolingvery fast; built into every languageslower; needs a library
Size of our sample182 bytes129 bytes

Errors you will meet

JSON: comments and trailing commas are syntax errors. JSON.parse('{"a":1, // x\n}') failed in our test with “Expected double-quoted property name in JSON at position 8”. Formats such as JSON5 or JSONC allow comments, but plain JSON parsers reject them.

YAML: indentation must use spaces. A tab produced “tab characters must not be used in indentation (2:1)” in js-yaml, with the line and column of the offending tab.

Which to use

  • APIs, logs, data between services: JSON. It is strict, fast and unambiguous.
  • Config edited by people (CI pipelines, Kubernetes manifests, app settings): YAML, if your tooling expects it, with values such as versions, country codes, octal-looking numbers and yes/no answers quoted.
  • Config read by many languages: JSON, or YAML restricted to a safe subset and validated with a schema.

Many YAML tools accept JSON directly, because JSON’s syntax is valid YAML 1.2 in practice. To move between the two, use the YAML to JSON and JSON to YAML converters; the second quotes risky strings for you so the round trip is lossless. A related parsing pitfall, strings that look like numbers, is covered in the Python string-to-int guide.

Sources

  1. YAML Ain't Markup Language, version 1.2.2 — yaml.org
  2. RFC 8259: The JSON Data Interchange Format — IETF
  3. PyYAML Documentation — PyYAML
  4. js-yaml — nodeca on GitHub

How we made this

Reproduced in a local lab. We parsed the same YAML file with PyYAML 6.0.3 (yaml.safe_load) and js-yaml 4.1.0 (load), and the equivalent JSON with JSON.parse in Node.js 25. Outputs are quoted verbatim, re-serialized as JSON. The interactive example runs the same js-yaml version in your browser.

Performed by Claude, an AI assistant working in the BinConvert repository (Python 3.14.3 + PyYAML 6.0.3, Node.js 25.8.2 + js-yaml 4.1.0), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; both parsers were re-run on the file and their output compared with the tables. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!