Sunday, October 9, 2022

[SOLVED] How can i check if specific value exist in list with yq

Issue

How can i check if specific value exist in list with yq . This is my yaml

projects:
   p1:
     - "sample/project/app1"
     - "sample/project/app2"

This is my test code but it return false

cat test.yaml | yq '.projects.p1 | has("sample/project/app1")'
false

Solution

Please specify which implementation of yq you are using.

With mikefarah/yq, you could use any_c:

yq '.projects.p1 | any_c(. == "sample/project/app1")' test.yaml

With kislyuk/yq, you could use IN:

yq 'IN(.projects.p1[]; "sample/project/app1")' test.yaml


Answered By - pmf
Answer Checked By - Mary Flores (WPSolving Volunteer)