YAML to JSON: Anchors, Merge Keys and Why yes Is Text
Short answer: to convert YAML to JSON, know that YAML is the usual format for configuration in Kubernetes, Docker Compose and GitHub Actions, while JSON is what APIs and programs exchange. The YAML ⇄ JSON Converter converts in both directions, supports anchors, aliases, merge keys, block scalars and multi-document files, and gives you the line and column when the input has an error.
A worked example
base: &base
retries: 3
timeout: 30
prod:
<<: *base
timeout: 60
flags:
debug: yes
region: "eu-west-1"
---
second: doc
converts to JSON like this:
- the anchor
&baseis expanded; - the merge key
<<: *basecopiesretriesintoprod, and the key written inprod(timeout: 60) overrides the merged value; - the two documents separated by
---become a JSON array.
Why yes becomes the string "yes"
The tool follows YAML 1.2 rules: yes, no and on are text, not booleans, and 0o17 is an octal number. Some older tools (YAML 1.1) read them differently, so debug: yes becomes true there. If a config behaves differently in two tools, this is a likely reason.
JSON to YAML: why some strings are quoted
When converting to YAML, strings that a parser could read as something else are quoted so the data survives a round trip. This JSON:
{"enabled":"yes","version":"1.10","port":"8080","empty":"","note":"a: b"}
becomes:
enabled: "yes"
version: "1.10"
port: "8080"
empty: ""
note: "a: b"
Without quotes, yes would be read as a boolean, 1.10 as the number 1.1 and 8080 as an integer.
Normalize YAML
Normalize YAML rewrites a document in a consistent style. It cannot keep comments, and it expands anchors, so review the result before replacing a file. JSON has no comments either, so a YAML → JSON conversion drops them.
What is not supported
Custom tags such as !Ref are ignored, and explicit types like !!binary are not decoded. JSON cannot hold .inf or .nan, so those are reported. Duplicate keys and tab indentation are reported as errors. For the usual YAML mistakes, see YAML syntax errors.
How to use the converter
- Open the YAML ⇄ JSON Converter.
- Choose YAML to JSON, JSON to YAML or Normalize YAML.
- Paste the input and fix any reported line and column.
- Copy the result. Validate JSON with the JSON Validator (errors are explained in JSON syntax errors), and compare two versions with JSON Diff.
For other formats see the .env ⇄ JSON Converter and the guide to .env conversion, the TOML guide and JSON ⇄ TOON.
Limits to know
- Comments are lost in every conversion.
- Anchors are expanded, so the output is larger than the input.
- Custom tags are ignored.
Frequently asked questions
Are comments kept when I convert? No.
How are multiple YAML documents converted? As a JSON array.
Are anchors and merge keys supported? Yes, they are expanded.
Why is yes a string?
YAML 1.2 treats it as text.
Is my data uploaded? No.