Home > Enterprise >  Detect empty json values
Detect empty json values

Time:09-28

I'm trying to detect when I get all empty json values.

Here are 2 examples of empty json values.

json='{
  "data": [],
  "pagination": {
    "cursor": ""
  }
}'

json='{
    "data": [],
    "error": "",
    "message": "",
    "status": ""
}'

The closest I've gotten is this, which seems to work for the first example, but not the 2nd.

echo "$channel_json" | jq -e 'all( .[] ; . == "" or . == [] or . == {}  )'

One, I thought easy way, would be to get the string text from each key pair, then just check if I'm left with an empty string. But I haven't found a way that works in the pagination example.

CodePudding user response:

This will give you false if all strings and arrays are "" or []:

jq -e 'all(.. | strings, arrays; IN("", [])) | not'

Demo

  • Related