Variable Operators

Like the jq equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields).

Note that there is also an additional ref operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.

Single value variable

Given a sample.yml file of:

a: cat

then

yq '.a as $foo | $foo' sample.yml

will output

cat

Multi value variable

Given a sample.yml file of:

- cat
- dog

then

yq '.[] as $foo | $foo' sample.yml

will output

Using variables as a lookup

Example taken from jq

Given a sample.yml file of:

then

will output

Using variables to swap values

Given a sample.yml file of:

then

will output

Use ref to reference a path repeatedly

Note: You may find the with operator more useful.

Given a sample.yml file of:

then

will output

Last updated

Was this helpful?