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
| Concept | YAML | JSON |
|---|---|---|
| Structure | Indentation and newlines | Braces and commas |
| Key/value | name: Ada | "name": "Ada" |
| List | tags: - a - b | "tags": ["a", "b"] |
| String | Optional quotes | Always double quotes |
| Comments | # note | None (JSONC only) |
| Multi-line text | | and > blocks | Escaped 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 input | Common result in JSON | Intent |
|---|---|---|
| enabled: yes | "enabled": true | Boolean, or wanted the string? |
| mode: on | "mode": true | Often meant as text |
| version: 1.10 | "version": 1.1 | Trailing zero dropped |
| code: 012 | "code": 10 | Read as octal by YAML 1.1 |
| name: | "name": null | Empty 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
- Validate YAML before merge — one indentation error fails the whole pipeline.
- Convert to JSON to diff structurally:
kubectl get deploy myapp -o jsongives machine-comparable output. - Use dry runs (
kubectl apply --dry-run=client -o yaml) to see exactly what a manifest expands to. - Keep manifests in YAML for humans and let controllers parse them; avoid hand-editing JSON.
- 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.