Running the below:
$ echo '["lorem", "ipsum", "dolor"]' | jq '. | contains(["rem"])'
returns true
because "rem" is a substring of "lorem". I would like a whole word match, which would disqualify "rem" and only allow one of the three values in the input.
$ echo '["lorem", "ipsum", "dolor"]' | jq 'select(. | index("rem"))'
returns nothing at all, but jq exits cleanly. I would have expected a false
.
How do I get a boolean value for whole word match?
2
Answers
You want to receive
true
ifany
(=at least one) of the items equals (==
) a given value:In other words, you want to receive
true
if a given value is containedIN
a set of items.[]
:I’ve tripped over this many times before as well, as you’d expect it to work like this out of the box.
But the answer (if you’re after a chainable exit code) is: use the
-e
flag, which, according toman jq
:So you’d want
jq -e 'select(. | index("rem"))'