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 (v4.47.1 #2110)
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:
then
will output
Optional Splat
Just like splat, but won't error if you run it against scalars
Given a sample.yml file of:
then
will output
Special characters
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
then
will output
Nested special characters
Given a sample.yml file of:
then
will output
Keys with spaces
Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
then
will output
Dynamic keys
Expressions within [] can be used to dynamically lookup / calculate keys
Given a sample.yml file of:
then
will output
Children don't exist
Nodes are added dynamically while traversing
Given a sample.yml file of:
then
will output
Optional 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:
then
will output
Wildcard matching
Given a sample.yml file of:
then
will output
Aliases
Given a sample.yml file of:
then
will output
Traversing aliases with splat
Given a sample.yml file of:
then
will output
Traversing aliases explicitly
Given a sample.yml file of:
then
will output
Traversing arrays by index
Given a sample.yml file of:
then
will output
Traversing nested arrays by index
Given a sample.yml file of:
then
will output
Maps with numeric keys
Given a sample.yml file of:
then
will output
Maps with non existing numeric keys
Given a sample.yml file of:
then
will output
Traversing merge anchors
Given a sample.yml file of:
then
will output
Traversing merge anchors with local override
Given a sample.yml file of:
then
will output
Select multiple indices
Given a sample.yml file of:
then
will output
LEGACY: Traversing merge anchors with override
This is legacy behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
then
will output
LEGACY: 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:
then
will output
LEGACY: Splatting merge anchors
With legacy override behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
then
will output
LEGACY: Splatting merge anchor lists
With legacy override behaviour, see --yaml-fix-merge-anchor-to-spec
Given a sample.yml file of:
then
will output
FIXED: 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:
then
will output
FIXED: 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:
then
will output
FIXED: 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:
then
will output
FIXED: 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:
then
will output
Last updated
Was this helpful?