Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Detect empty json values

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

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

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading