Skip to content
All posts

How to convert JSON to YAML (and back) without breaking things

JSON ↔ YAML looks simple until you hit anchors, multi-line strings, booleans that aren't booleans, and the Norway problem.

DDDev DeskDeveloper Tools EditorPublished April 24, 20265 min readbeginner

# The 30-second version

1. Paste JSON into the left panel of our JSON to YAML converter.

2. Copy the YAML from the right panel.

3. Check it with a parser before committing (see "Gotchas" below).

For machine-generated JSON (API responses, npm package.json, terraform state), one-way conversion is almost always safe. Hand-written YAML with anchors or tags is trickier because JSON can't represent those natively.

# Why you might need this

  • Your CI config is YAML but your API returns JSON.
  • Kubernetes manifests are YAML but kubectl get -o json gives you JSON.
  • You want to review a large JSON blob and YAML's indentation is easier to read.
  • A migration from Jenkins (YAML) to a JSON-config CI.

# The basic conversion


{
  "name": "atc",
  "tags": ["web", "converter"],
  "config": { "darkMode": true, "port": 8080 }
}

becomes


name: atc
tags:
  - web
  - converter
config:
  darkMode: true
  port: 8080

Done. Most real-world JSON converts this cleanly.

# Gotchas that bite in production

<div class="callout callout-warning" role="note"><div class="callout-title">Warning</div><div class="callout-body"><p>The Norway problem: unquoted <code>NO</code> is parsed as <code>false</code> by some YAML 1.1 parsers. If your data contains country codes, always quote them.</p></div></div>

  • Numbers that look like strings"00123" round-trips to 00123 and may get re-parsed as octal 83. Quote them.
  • Floating-point"0.1" JSON → 0.1 YAML → back to JSON might become 0.1 or 0.09999999999999998 depending on the parser. Keep critical numbers as strings.
  • Multi-line strings — YAML has | (literal, keep newlines) and > (folded, collapse newlines). JSON has \n. Round-tripping \n isn't always idempotent.
  • Keys that aren't strings — YAML supports complex keys (arrays, maps as keys). JSON doesn't. You can't round-trip these.

# When JSON → YAML is lossy

  • YAML anchors (&name and *name) — these are references to avoid duplication. JSON has no equivalent, so conversion has to inline them.
  • YAML tags (!!timestamp, !!binary) — JSON doesn't know types beyond string/number/bool/null/array/object.
  • YAML comments (# hello) — JSON has no comment syntax. They're silently dropped.

If the source is hand-written YAML with any of these features, round-tripping through JSON will lose information.

# When YAML → JSON is lossy

Never — JSON is a proper subset of YAML 1.2. Anything that's valid JSON is valid YAML; anything that's valid YAML that's not valid JSON won't appear in a JSON output.

# Checking your output

  • Paste JSON output back into our JSON Formatter to validate.
  • For YAML, any yamllint install will catch indentation bugs instantly.
  • In CI, run kubectl apply --dry-run=client -f for Kubernetes manifests.

Frequently asked questions

What is the Norway problem?

YAML 1.1 parses `NO` as boolean `false` (country codes!). YAML 1.2 fixed it — only `true`/`false` are booleans now — but many parsers still default to 1.1. Always quote country codes and anything that looks boolean-ish.

Can YAML hold everything JSON can?

Yes, and then some. YAML is a strict superset — every JSON document is valid YAML. Going the other way, you lose YAML-specific features like anchors, tags, and multi-line strings.

Why does my JSON have trailing commas but YAML doesn't need commas?

JSON uses commas to separate items; YAML uses newlines + indentation. YAML's syntax is lighter at the cost of whitespace-sensitivity.

Haftada bir kez yeni gönderiler.

Pratik geliştirici rehberleri. Spam yok. İstediğiniz zaman abonelikten çıkabilirsiniz.

Tools mentioned

Keep reading