Home > Mobile >  How can i check if specific value exist in list with yq
How can i check if specific value exist in list with yq

Time:10-01

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

CodePudding user response:

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

CodePudding user response:

You cannot check whether array contains a string with has(). Please try instead:

yq < test.yaml '.projects.p1 | index("sample/project/app1") != null'
true
  • Related