Anchor and Alias Operators
Use the alias and anchor operators to read and write yaml aliases and anchors. The explode operator normalises a yaml file (dereference (or expands) aliases and remove anchor names).
yq supports merge aliases (like <<: *blah) however this is no longer in the standard yaml spec (1.2) and so yq will automatically add the !!merge tag to these nodes as it is effectively a custom tag.
NOTE --yaml-fix-merge-anchor-to-spec flag
yq doesn't merge anchors <<: to spec, in some circumstances it incorrectly overrides existing keys when the spec documents not to do that.
To minimise disruption while still fixing the issue, a flag has been added to toggle this behaviour. This will first default to false; and log warnings to users. Then it will default to true (and still allow users to specify false if needed).
This flag also enables advanced merging, like inline maps, as well as fixes to ensure when exploding a particular path, neighbours are not affect ed.
Long story short, you should be setting this flag to true.
See examples of the flag differences below, where LEGACY is with the flag off; and FIXED is with the flag on.
Merge one map
see https://yaml.org/type/merge.html
Given a sample.yml file of:
- &CENTER
x: 1
y: 2
- &LEFT
x: 0
y: 2
- &BIG
r: 10
- &SMALL
r: 1
- !!merge <<: *CENTER
r: 10then
will output
Get anchor
Given a sample.yml file of:
then
will output
Set anchor
Given a sample.yml file of:
then
will output
Set anchor relatively using assign-update
Given a sample.yml file of:
then
will output
Get alias
Given a sample.yml file of:
then
will output
Set alias
Given a sample.yml file of:
then
will output
Set alias to blank does nothing
Given a sample.yml file of:
then
will output
Set alias relatively using assign-update
Given a sample.yml file of:
then
will output
Explode alias and anchor
Given a sample.yml file of:
then
will output
Explode with no aliases or anchors
Given a sample.yml file of:
then
will output
Explode with alias keys
Given a sample.yml file of:
then
will output
Dereference and update a field
Use explode with multiply to dereference an object
Given a sample.yml file of:
then
will output
LEGACY: Explode with merge anchors
Caution: this is for when --yaml-fix-merge-anchor-to-spec=false; it's not to YAML spec because the merge anchors incorrectly override the object values (foobarList.b is set to bar_b when it should still be foobarList_b). Flag will default to true in late 2025
Given a sample.yml file of:
then
will output
LEGACY: Merge multiple maps
see https://yaml.org/type/merge.html. This has the correct data, but the wrong key order; set --yaml-fix-merge-anchor-to-spec=true to fix the key order.
Given a sample.yml file of:
then
will output
LEGACY: Override
see https://yaml.org/type/merge.html. This has the correct data, but the wrong key order; set --yaml-fix-merge-anchor-to-spec=true to fix the key order.
Given a sample.yml file of:
then
will output
FIXED: Explode with merge anchors
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour (flag will default to true in late 2025). Observe that foobarList.b property is still foobarList_b.
Given a sample.yml file of:
then
will output
FIXED: Merge multiple maps
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour (flag will default to true in late 2025). Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.
Given a sample.yml file of:
then
will output
FIXED: Override
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour (flag will default to true in late 2025). Taken from https://yaml.org/type/merge.html. Same values as legacy, but with the correct key order.
Given a sample.yml file of:
then
will output
Exploding inline merge anchor
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour (flag will default to true in late 2025).
Given a sample.yml file of:
then
will output
Last updated
Was this helpful?