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: applethen
yq 'first(.a == "cat")' sample.ymlwill output
a: catFirst matching element from array with multiple matches
Given a sample.yml file of:
- a: banana
- a: cat
  b: firstCat
- a: apple
- a: cat
  b: secondCatthen
yq 'first(.a == "cat")' sample.ymlwill output
a: cat
b: firstCatFirst matching element from array with numeric condition
Given a sample.yml file of:
- a: 10
- a: 100
- a: 1
- a: 101then
yq 'first(.a > 50)' sample.ymlwill output
a: 100First matching element from array with boolean condition
Given a sample.yml file of:
- a: false
- a: true
  b: firstTrue
- a: false
- a: true
  b: secondTruethen
yq 'first(.a == true)' sample.ymlwill output
a: true
b: firstTrueFirst matching element from array with null values
Given a sample.yml file of:
- a: null
- a: cat
- a: applethen
yq 'first(.a != null)' sample.ymlwill output
a: catFirst matching element from array with complex condition
Given a sample.yml file of:
- a: dog
  b: 7
- a: cat
  b: 3
- a: apple
  b: 5then
yq 'first(.b > 4 and .b < 6)' sample.ymlwill output
a: apple
b: 5First matching element from map
Given a sample.yml file of:
x:
  a: banana
y:
  a: cat
z:
  a: applethen
yq 'first(.a == "cat")' sample.ymlwill output
a: catFirst matching element from map with numeric condition
Given a sample.yml file of:
x:
  a: 10
y:
  a: 100
z:
  a: 101then
yq 'first(.a > 50)' sample.ymlwill output
a: 100First matching element from nested structure
Given a sample.yml file of:
items:
  - a: banana
  - a: cat
  - a: applethen
yq '.items | first(.a == "cat")' sample.ymlwill output
a: catFirst matching element with no matches
Given a sample.yml file of:
- a: banana
- a: cat
- a: applethen
yq 'first(.a == "dog")' sample.ymlwill output
First matching element from empty array
Given a sample.yml file of:
[]then
yq 'first(.a == "cat")' sample.ymlwill output
First matching element from scalar node
Given a sample.yml file of:
hellothen
yq 'first(. == "hello")' sample.ymlwill output
First matching element from null node
Given a sample.yml file of:
nullthen
yq 'first(. == "hello")' sample.ymlwill output
First matching element with string condition
Given a sample.yml file of:
- a: banana
- a: cat
- a: applethen
yq 'first(.a | test("^c"))' sample.ymlwill output
a: catFirst matching element with length condition
Given a sample.yml file of:
- a: hi
- a: hello
- a: worldthen
yq 'first(.a | length > 4)' sample.ymlwill output
a: helloFirst matching element from array of strings
Given a sample.yml file of:
- banana
- cat
- applethen
yq 'first(. == "cat")' sample.ymlwill output
catFirst matching element from array of numbers
Given a sample.yml file of:
- 10
- 100
- 1then
yq 'first(. > 50)' sample.ymlwill output
100First element with no filter from array
Given a sample.yml file of:
- 10
- 100
- 1then
yq 'first' sample.ymlwill output
10First element with no filter from array of maps
Given a sample.yml file of:
- a: 10
- a: 100then
yq 'first' sample.ymlwill output
a: 10Last updated
Was this helpful?