Path

The path operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.

You can get the key/index of matching nodes by using the path operator to return the path array then piping that through .[-1] to get the last element of that array, the key.

Use setpath to set a value to the path array returned by path, and similarly delpaths for an array of path arrays.

Map path

Given a sample.yml file of:

a:
  b: cat

then

yq '.a.b | path' sample.yml

will output

- a
- b

Get map key

Given a sample.yml file of:

a:
  b: cat

then

will output

Array path

Given a sample.yml file of:

then

will output

Get array index

Given a sample.yml file of:

then

will output

Given a sample.yml file of:

then

will output

Set path

Given a sample.yml file of:

then

will output

Set on empty document

Running

will output

Set path to prune deep paths

Like pick but recursive. This uses ireduce to deeply set the selected paths into an empty object.

Given a sample.yml file of:

then

will output

Set array path

Given a sample.yml file of:

then

will output

Set array path empty

Running

will output

Delete path

Notice delpaths takes an array of paths.

Given a sample.yml file of:

then

will output

Delete array path

Given a sample.yml file of:

then

will output

Delete - wrong parameter

delpaths does not work with a single path array

Given a sample.yml file of:

then

will output

Last updated

Was this helpful?