Developer Tools

YAML to JSON: Converting Config Files Safely

Convert YAML to JSON without surprises: syntax differences, indentation rules, type coercion pitfalls like yes and no, lost comments, and CI and Kubernetes usage.

3 min read·Updated 2026-07-24

YAML dominates configuration — GitHub Actions, Docker Compose, Ansible and Kubernetes all speak it — while APIs and tooling prefer JSON. Converting between them is routine, but YAML carries behaviours JSON does not: indentation is syntax, some words silently become booleans, and comments vanish on the way out. This guide shows how to convert safely.

Syntax differences at a glance

ConceptYAMLJSON
StructureIndentation and newlinesBraces and commas
Key/valuename: Ada"name": "Ada"
Listtags:
- a
- b
"tags": ["a", "b"]
StringOptional quotesAlways double quotes
Comments# noteNone (JSONC only)
Multi-line text| and > blocksEscaped in one string

YAML is a serialisation of the same data model JSON uses, so a correct conversion is a re-formatting, not a transformation. The YAML to JSON Converter parses YAML properly instead of doing text substitution.

Indentation pitfalls

  • Spaces only. A tab is a parse error in most YAML parsers.
  • Consistent width. Two or four spaces per level, never mixed within one document.
  • Lists under a key are indented one level deeper, or aligned with the key — both work, mixing them does not.
  • Colons in values need quoting: url: "https://example.com:8080".
  • Special starters — values beginning with @, %, or - must be quoted.

Type coercion: yes, no and friends

This is where conversions silently corrupt data. YAML 1.1 treats a set of bare words as non-strings:

YAML inputCommon result in JSONIntent
enabled: yes"enabled": trueBoolean, or wanted the string?
mode: on"mode": trueOften meant as text
version: 1.10"version": 1.1Trailing zero dropped
code: 012"code": 10Read as octal by YAML 1.1
name:"name": nullEmpty scalar becomes null

Quote anything ambiguous — "yes", "012", "1.10" — in the YAML source. Once converted, the damage is baked into the JSON and no longer visible.

Comments are lost — plan for it

JSON has no comment syntax, so every explanation an engineer wrote in the YAML disappears during conversion. Handle it one of three ways: keep the YAML as the documented source of truth and generate JSON for machines; move explanations into a sibling README or a _comment key that consumers ignore; or use JSON5/JSONC if your consumer accepts them.

Using conversions in CI and Kubernetes

  1. Validate YAML before merge — one indentation error fails the whole pipeline.
  2. Convert to JSON to diff structurally: kubectl get deploy myapp -o json gives machine-comparable output.
  3. Use dry runs (kubectl apply --dry-run=client -o yaml) to see exactly what a manifest expands to.
  4. Keep manifests in YAML for humans and let controllers parse them; avoid hand-editing JSON.
  5. Round-trip back with the JSON to YAML Converter when an API returns JSON you want to commit as a manifest.

Frequently asked questions

Is the conversion always lossless? Structurally yes, semantically no — comments, anchors and explicit formatting preferences are lost, and unquoted ambiguous scalars may change type.

Why does my JSON have true where I wrote "on"? The parser applied YAML 1.1 boolean rules. Quote the value in the source YAML and convert again.

Can I keep comments through conversion? Not in standard JSON. Preserve them alongside the file, or keep YAML as the authored version and treat JSON as a build artifact.

Tools used in this guide

Related guides