I have a JSON array output from an API, which I am interpreting with the jq
package. I want to see if a target value exists in this array. I am quite new to shell scripting and am using one of the simpler approaches described in this post. It works when I type the target value in manually, but not when I set it as a variable.
in_stock=$(jq -n --argjson v '["orange","apple","pickle"]' '$v')
echo $in_stock | jq 'contains(["pickle"])'
>>> true
echo $in_stock | jq 'contains(["spinach"])'
>>> false
favorite_food="pickle"
echo $in_stock | jq 'contains(["$favorite_food"])'
>>> false
I assume this is a basic syntax issue on my part. I've tried a number of variations and it outputs false
. What am I doing wrong?