Is there a better way to pass string output from jq to bash?

I just opened jq and really loved it. One thing I find myself doing a lot, but kind of like:

result=$(jq --raw-output '.some | .filters // ""')
if [[ $result ]]; then
    foo
else
    bar
fi

By default, an empty string seems to play more nicely with bash "truth" than, for example. if [[ $result != "null" ]], and raw output is usually needed to store only the resulting string in a variable. My question is that I use these two settings so sequentially in the scripts, maybe the best way to achieve the same functionality? Or would it be prudent (as a possible jq boost) to be able to set up a couple of env vars to control this behavior throughout the script time?

+4
source share
1 answer

-e, jq 0, false, null, :

result=$(jq -e -r '.some | .filters') && foo || bar
+6

Source: https://habr.com/ru/post/1598461/


All Articles