Search code examples
yamlyq

Find the parent yaml node of an array element using yq


Assuming I have a yaml file more or less like this

foo:
 - element1
 - element2
bar: 
 - element3
 - element4

What is the yq command to get the parent of element4 (I want to get bar)


Solution

  • to_entries | .[] | select(.value[] | contains("element4")).key
    

    Will loop over the result of to_entries, search where .value contains element4, and logs .key of the result.