3 ms·
One feature I found very useful when using jq in Bash is the "alternative operator" '//'. result=$(echo "$data" | jq -r '.optional // ""') if [[ -n "$resul
by Calzifer 3y ago
One feature I found very useful when using jq in Bash is the "alternative operator" '//'.
result=$(echo "$data" | jq -r '.optional // ""')
if [[ -n "$result" ]] ...
feels more natural in Bash than
result=$(echo "$data" | jq -r '.optional')
if [[ "$result" != null ]] ...
Especially if an empty field should be handled the same way.
And when using the raw-output option it helps with the ambiguity between "null" and null.
- city41 3y agoSince it's a JSON tool, I wish it leaned more on JavaScript syntax. In this case I wish it was || or even better, ??
- llimllib 3y agoI suspect they didn't use || because it makes parsing easier, given jq's reliance on the pipe operator. It doesn't use ?? because it predates that operator's introduction into the language by about 8 years