First Operator

Returns the first matching element in an array, or first matching value in a map.

Can be given an expression to match with, otherwise will just return the first.

First matching element from array

Given a sample.yml file of:

- a: banana
- a: cat
- a: apple

then

yq 'first(.a == "cat")' sample.yml

will output

a: cat

First matching element from array with multiple matches

Given a sample.yml file of:

- a: banana
- a: cat
  b: firstCat
- a: apple
- a: cat
  b: secondCat

then

will output

First matching element from array with numeric condition

Given a sample.yml file of:

then

will output

First matching element from array with boolean condition

Given a sample.yml file of:

then

will output

First matching element from array with null values

Given a sample.yml file of:

then

will output

First matching element from array with complex condition

Given a sample.yml file of:

then

will output

First matching element from map

Given a sample.yml file of:

then

will output

First matching element from map with numeric condition

Given a sample.yml file of:

then

will output

First matching element from nested structure

Given a sample.yml file of:

then

will output

First matching element with no matches

Given a sample.yml file of:

then

will output

First matching element from empty array

Given a sample.yml file of:

then

will output

First matching element from scalar node

Given a sample.yml file of:

then

will output

First matching element from null node

Given a sample.yml file of:

then

will output

First matching element with string condition

Given a sample.yml file of:

then

will output

First matching element with length condition

Given a sample.yml file of:

then

will output

First matching element from array of strings

Given a sample.yml file of:

then

will output

First matching element from array of numbers

Given a sample.yml file of:

then

will output

First element with no filter from array

Given a sample.yml file of:

then

will output

First element with no filter from array of maps

Given a sample.yml file of:

then

will output

Last updated

Was this helpful?