How does one remove blank string items from an array in jq?
This is my best guess, but it doesn't appear to work:
Attempt
echo '["bob","","tim",""]' | jq '[ . [] | if length > 0 then . end ]'
Desired output:
["bob", "tim"]
Error:
. [] | if length > 0 then . end
jq: error: Possibly unterminated 'if' statement at <top-level>, line 1:
. [] | if length > 0 then . end
jq: 2 compile errors
Adding "else empty" gets the right result
jq '[ .[] | if length > 0 then . else empty end ]'
Consider using select instead.
jq '[ .[] | select(length > 0) ]'
And since map(x) is equivalent to [.[] | x]
, we can do this.
jq 'map(select(length > 0))'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With