0

I have an array of string in json format. I'd like to check whether it includes a specific string value.

I have tried to use contains:

echo '["aa", "bb", "cc"]' |jq '. | contains(["aa"])'
> true

it works but it also return true for a partial match:

echo '["aa", "bb", "cc"]' |jq '. | contains(["a"])'
> true

how can I find the exact match?

0

1 Answer 1

1

You could use a regex with a word boundary:

$ echo '["aa", "bb", "cc"]' | jq -r '.[] | test(["\\baa\\b"]) | tostring | select(. == "true")'
true

$ echo '["aa", "bb", "cc"]' | jq -r '.[] | test(["\\ba\\b"]) | tostring | select(. == "true")'

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.