Search code examples
jsonshelljqwhitelist

How to check if there is an exact match in the jq list


I am trying to find a tag that matches a particular image repository in the Docker registry.

curl -X GET http://my-registry-ip-address/v2/ancean-api/tags/list\

result: {"name":"ancean-api","tags":["v1.0.0","v19.0.0","v11.1.1"]}

How do I check if the v1.0.0(example) tag exists in the json data output as a result above?


I tried

curl -X GET http://my-registry-ip-address/v2/ancean-api/tags/list | jq '.tags | select(. == v1.0.19)'

jq document The jq document attempted to use select but failed. The above command is successful regardless of which tag is used.

curl -X GET http://my-registry-ip-address/v2/ancean-api/tags/list | jq '.tags | .[]' | grep "v1.0.0"

I tried to use it with grep but failed.


Problem Solved

Through pms's answer, I solved the problem with the IN function.

curl -X GET http://my-registry-ip-address/v2/ancean-api/tags/list | jq IN'(.tags[]; "v1.0.0")'

Using IN, I was able to get the bool value back when there was a specific value in the json data. enter image description here


Solution

  • If the object containing the tags key lives inside another object's default key (as suggested by your example, although the quotes are missing), you have to include that in the path.

    There, you can use IN function to test for the containedness of the item in question, which produces a boolean:

    IN(.default.tags[]; "v1.0.0")
    

    Additionally, the --exit-status (or -e) flag can set the exit status dependin on the result.