Traverse (Read)
This is the simplest (and perhaps most used) operator. It is used to navigate deeply into yaml structures.
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)
See examples of the flag differences below, where LEGACY is the flag off; and FIXED is with the flag on.
Simple map navigation
Given a sample.yml file of:
a:
b: applethen
yq '.a' sample.ymlwill output
b: appleSplat
Often used to pipe children into other operators
Given a sample.yml file of:
- b: apple
- c: bananathen
yq '.[]' sample.ymlwill output
b: apple
c: bananaOptional Splat
Just like splat, but won't error if you run it against scalars
Given a sample.yml file of:
catthen
yq '.[]' sample.ymlwill output
Special characters
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
"{}": frogthen
yq '.["{}"]' sample.ymlwill output
frogNested special characters
Given a sample.yml file of:
a:
"key.withdots":
"another.key": applethen
yq '.a["key.withdots"]["another.key"]' sample.ymlwill output
appleKeys with spaces
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
"red rabbit": frogthen
yq '.["red rabbit"]' sample.ymlwill output
frogDynamic keys
Expressions within [] can be used to dynamically lookup / calculate keys
Given a sample.yml file of:
b: apple
apple: crispy yum
banana: soft yumthen
yq '.[.b]' sample.ymlwill output
crispy yumChildren don't exist
Nodes are added dynamically while traversing
Given a sample.yml file of:
c: bananathen
yq '.a.b' sample.ymlwill output
nullOptional identifier
Like jq, does not output an error when the yaml is not an array or object as expected
Given a sample.yml file of:
- 1
- 2
- 3then
yq '.a?' sample.ymlwill output
Wildcard matching
Given a sample.yml file of:
a:
cat: apple
mad: thingsthen
yq '.a."*a*"' sample.ymlwill output
apple
thingsAliases
Given a sample.yml file of:
a: &cat
c: frog
b: *catthen
yq '.b' sample.ymlwill output
*catTraversing aliases with splat
Given a sample.yml file of:
a: &cat
c: frog
b: *catthen
yq '.b[]' sample.ymlwill output
frogTraversing aliases explicitly
Given a sample.yml file of:
a: &cat
c: frog
b: *catthen
yq '.b.c' sample.ymlwill output
frogTraversing arrays by index
Given a sample.yml file of:
- 1
- 2
- 3then
yq '.[0]' sample.ymlwill output
1Traversing nested arrays by index
Given a sample.yml file of:
[[], [cat]]then
yq '.[1][0]' sample.ymlwill output
catMaps with numeric keys
Given a sample.yml file of:
2: catthen
yq '.[2]' sample.ymlwill output
catMaps with non existing numeric keys
Given a sample.yml file of:
a: bthen
yq '.[0]' sample.ymlwill output
nullTraversing merge anchors
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar.a' sample.ymlwill output
foo_aTraversing merge anchors with local override
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar.thing' sample.ymlwill output
foobar_thingSelect multiple indices
Given a sample.yml file of:
a:
- a
- b
- cthen
yq '.a[0, 2]' sample.ymlwill output
a
cLEGACY: Traversing merge anchors with override
This is legacy behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar.c' sample.ymlwill output
foo_cLEGACY: Traversing merge anchor lists
Note that the later merge anchors override previous, but this is legacy behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobarList.thing' sample.ymlwill output
bar_thingLEGACY: Splatting merge anchors
With legacy override behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar[]' sample.ymlwill output
foo_c
foo_a
foobar_thingLEGACY: Splatting merge anchor lists
With legacy override behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobarList[]' sample.ymlwill output
bar_b
foo_a
bar_thing
foobarList_cFIXED: Traversing merge anchors with override
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour.
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar.c' sample.ymlwill output
foobar_cFIXED: Traversing merge anchor lists
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour. Note that the keys earlier in the merge anchors sequence override later ones
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobarList.thing' sample.ymlwill output
foo_thingFIXED: Splatting merge anchors
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour. Note that the keys earlier in the merge anchors sequence override later ones
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobar[]' sample.ymlwill output
foo_a
foobar_thing
foobar_cFIXED: Splatting merge anchor lists
Set --yaml-fix-merge-anchor-to-spec=true to get this correct merge behaviour. Note that the keys earlier in the merge anchors sequence override later ones
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thingthen
yq '.foobarList[]' sample.ymlwill output
foobarList_b
foo_thing
foobarList_c
foo_aLast updated
Was this helpful?