tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

Check if flattened array is empty #214

Closed calvinmo closed 3 years ago

calvinmo commented 3 years ago

Hello

Is it possible to detect if a flattened array is empty? ie:

Failure scenario:

[
{ "partitions": ["rabbit1", "rabbit2"] },
{ "partitions": ["rabbit3"] },
{ "partitions": [] }
]

produces non-empty signal: ie false or >0.

Success scenario:

[
{ "partitions": [] },
{ "partitions": [] },
{ "partitions": [] }
]

produces empty signal: ie true or 0.

Critically, the output signal has to be a primitive value of one of the following types: boolean, number, string, null (this is due to my own constraints).

I have tried at length and I suspect this is not actually possible as this library stands.

Regards

Calvin

calvinmo commented 3 years ago

To answer my own question: Yes it can be done but not with version 1.1.3 (which is what I had). Once I upgraded to version 1.7.4 #.partitions.#(%"*")#|@flatten.# does the trick.