Skip to content
DevTinyTools

Converting

JSON to YAML: typing traps in YAML 1.2

JSON and YAML share the same underlying data model, but their readers disagree about unquoted scalars. Knowing which strings look like booleans, octal numbers or floats to a YAML parser is what keeps configuration files from changing value between environments.

8 min readTool: JSON to YAML

YAML 1.2 is a superset of JSON; YAML 1.1 is not

Developers often treat JSON and YAML as interchangeable spellings of the same config file. Under YAML 1.2 that is nearly true: every JSON value — objects, arrays, strings, numbers, booleans and null — maps cleanly to YAML. The trouble is that older tooling (including Python's default PyYAML loader and many legacy Helm/CI pipelines) still implements YAML 1.1.

When you omit quotes in YAML, the parser decides the type from the token's shape. That convenience is where almost every cross-format config bug starts.

Five typing traps when converting

How YAML 1.1 and YAML 1.2 interpret ambiguous unquoted scalars
YAML inputYAML 1.1YAML 1.2Safe practice
country: NOfalse (boolean)"NO" (string)Quote country: "NO" if older YAML 1.1 tools also read the file.
enabled: yestrue (boolean)"yes" (string)Use true/false for booleans, or quote "yes" for a literal string.
version: 1.201.2 (float)1.2 (float)Always quote versions and image tags: version: "1.20".
zip: 021341116 (octal int!)2134 (int)Quote leading-zero codes: zip: "02134".
port: "8080""8080" (string)"8080" (string)Drop quotes when you want a number: port: 8080.
# JSON input
{"service":"api","version":"1.20","enabled":"true","port":8080}

# Safe YAML 1.2 output (strings that look numeric or boolean stay quoted)
service: api
version: "1.20"
enabled: "true"
port: 8080

Comments, anchors and multi-document streams

  • Comments are dropped. JSON has no # comment syntax (that is JSONC or JSON5 — see why JSON5 fails in strict parsers). Keep comments in a YAML source file rather than round-tripping through JSON.
  • Anchors expand in place. &defaults and *defaults are resolved before JSON emission, so every consumer receives the expanded object.
  • Streams become arrays. Kubernetes manifests often separate objects with ---. A single-document YAML file converts to its root object; a multi-document file converts to a JSON array in file order.

Checking round-trips before you deploy

Tabs are forbidden for YAML indentation, and mixing two-space and four-space levels within the same mapping stops the parser. Whenever you edit a deployment manifest, run it through the JSON ↔ YAML converter and click Swap to confirm the round-trip returns identical types and values.

Frequently asked questions

Is every JSON file valid YAML?
Yes under YAML 1.2. The YAML 1.2 specification is a strict superset of JSON, so any valid JSON document parses cleanly in a YAML 1.2 reader. The reverse is false: YAML comments, unquoted keys, anchors and indentation are all syntax errors in JSON.
What is the YAML Norway problem?
Under YAML 1.1 (still used by PyYAML's default loader and older CI tools), unquoted yes, no, on, off, y and n are parsed as booleans. A country list containing NO for Norway silently becomes false. YAML 1.2 restricts booleans to true and false, fixing the bug.
Why did version: 1.20 turn into 1.2 in JSON?
Because unquoted 1.20 is a floating-point number in both YAML and JSON, and trailing decimal zeros carry no numeric meaning. Quote the value as "1.20" in YAML so both formats keep it as text.
What happens to multi-document YAML files with ---?
A single JSON file can hold only one top-level value, so a stream separated by --- markers cannot convert directly into multiple root objects. Clean converters collect every document into a single JSON array in file order.
Why do YAML comments and anchors disappear in JSON?
JSON has no comment syntax and no anchor/alias mechanism. Converting YAML to JSON expands aliases (&name / *name) into their values and drops # comments permanently, so keep your YAML source when comments matter.