YAML anchors and merge keys: what survives JSON
25 NOVEMBER 2026 · 8 MIN READ
YAML has three features that JSON cannot express — shared nodes, merge keys and typed scalars — and each one is handled differently depending on which spec edition your parser implements. Anchors expand everywhere; merge keys usually do not.
An anchor and its alias are the most useful thing YAML has over JSON, and the most confusing to convert, because the answer depends on what you mean. Does *b mean "paste the same value here" or "reference the same object in memory"? JSON can only answer the first question, so every YAML-to-JSON converter has to choose, and the two common libraries do not choose alike.
Aliases expand
common: &defaults
retries: 3
region: eu-west-1
worker: *defaults
cron: *defaults
-> {"common":{"retries":3,"region":"eu-west-1"},"worker":{"retries":3,"region":"eu-west-1"},"cron":{"retries":3,"region":"eu-west-1"}} The alias is resolved while loading, and the shared object is then written out twice, because JSON has no way to say "the same node". The identity is lost: mutate worker.retries in the converted document and cron.retries does not follow. Anything downstream that relied on that linkage — a watcher, a diff, a cache key — now sees two independent objects. For configuration this is usually the behaviour you wanted anyway; for data interchange it is a silent semantic change.
Merge keys do not expand
base: &b
host: api
port: 443
svc:
<<: *b
port: 8443
-> {"base":{"host":"api","port":443},"svc":{"<<":{"host":"api","port":443},"port":8443}} Read that output again. The key << is still there, as a literal member of svc, and svc.port is 8443 only because the override happened to sit after it. << is a YAML 1.1 feature that the 1.2 core schema dropped, and a parser implementing the core schema treats it as an ordinary key. Helm, Kustomize and PyYAML apply the merge, so their effective configuration is { host: api, port: 8443 }; a strict loader gives you the shape above. Convert the file, and you have changed which keys exist.
| Construct | YAML 1.1 parser | Core-schema parser |
|---|---|---|
&a / *a | shared node, expanded on serialise | same — aliases always resolve |
<<: *a | merged into the mapping | kept as a key named << |
yes, off | booleans | the strings "yes" and "off" |
2026-11-24 | a timestamp object | the string "2026-11-24" |
0x1f | 31 | 31, though a lenient fallback may keep "0x1f" |
.inf | infinity | infinity, which then becomes null in JSON |
Tags are the loud failure
!!binary, !!timestamp and any custom tag such as !myco/widget exist precisely because a plain scalar cannot carry its type. A parser restricted to the core types has nowhere to put them, so it raises — and the behaviour you then see depends on whether the reader gives up or degrades. Here the converter falls back to a permissive implementation that leaves the tag in the text, so v: !!binary aGk= arrives as { "v": "!!binary aGk=" }. That is not corruption so much as a decision: an unreadable tag becomes an ordinary string rather than an error, and you have to notice it yourself.
NOTE If a field that should hold decoded bytes instead starts with !!binary or !, your YAML never had a schema-compliant reader. Re-export it, or convert the encoding yourself — base64 payloads have their own page at Base64 to JSON.
Going the other way
JSON has no anchors, so the exporter never invents them: shared sub-objects are written out in full at every site. That is the safe direction, and it is also why a YAML file you convert and convert back has grown. Strings that would be misread get quoted on the way out — "1.0", "yes", "null" and "2026-11-24" all come back as the strings they were, which is the single most common round-trip bug in this pair of formats.
Two documents in one file
A stream separated by --- is not one big mapping. Strict readers either refuse it or hand you a list; the mistake is to assume a silent merge, because that produces a document that existed in no file. Here a multi-document stream converts to a JSON array of the documents, and a file with a single leading --- still converts to one object. If you want the Kubernetes habit of one manifest per document, remember that the array form is what a schema or an jq filter has to expect.
A short pre-flight list
- 01 Search for
<<:. If it appears, the merge matters and any conversion that keeps it as a literal key has changed your configuration. - 02 Search for
!!and for tags starting with a single!. Each one is a type that JSON cannot hold. - 03 Check that dates and
yes/noarrive as strings where the consumer expects strings, and as scalars where it expects booleans. - 04 Repeat keys are rejected by strict parsers and resolved last-wins by permissive ones; either way, a file with two identical keys at the same level is a bug, not a merge.
- 05 Then diff: the comparator on the converted pair will show you the exact keys that moved, appeared or disappeared.
None of this makes YAML the wrong choice — it makes the parser part of your format decision. Whichever tool reads the file is the one defining what your configuration means, and the two most common readers disagree about a feature that has been in the YAML spec since 1.1.