jq returning null as string if the json is empty

Something useful I found for shell scripts was:

jq '.foo // empty'

Which returns the match if successful, and the empty string if unsuccessful. So in bash I use:

addr=$(./xuez-cli getnetworkinfo | jq -r '.localaddresses[0].address // empty')

if [[ ! -z "$addr" ]]; then
    # do something
fi

Ref: https://github.com/stedolan/jq/issues/354#issuecomment-43147898
https://unix.stackexchange.com/questions/451479/jq-print-for-null-values

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)