0

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 "$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.

2
  • Check out stackoverflow.com/questions/7340060/… Commented Sep 27, 2022 at 14:36
  • That is a possibility, but I was hoping there was a more compact solution. Commented Sep 27, 2022 at 14:44

2 Answers 2

1

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

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

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was after! Thank you so much.
Note: Applying De Morgan's laws, the filter can also be expressed using any by bringing not inside to act on IN: any(.. | strings, arrays; IN("", []) | not)
1

This might be a good use for gron

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

gron transforms the input JSON into discrete paths:

$ gron <<< "$json"
json = {};
json.data = [];
json.pagination = {};
json.pagination.cursor = "";
json.pagination.position = [];
json.pagination.position[0] = 2;
json.pagination.position[1] = 3;

and to test for any non-empty elements

$ gron <<< "$json" | grep -Evq '(\{\}|\[\]|"");$' && echo "not everything is empty"
not everything is empty

1 Comment

Oh wow, gron is pretty cool, learn something new every day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.