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

Definition of boolean_expression in jq

According to the manual select needs a parameter boolean_expression.
I always wonder what exactly is meant by this in jq.
To take full advantage of the select filter, it would be nice to have a clear definition.

Can someone give this missing precise definition?

The following collection of unusual examples looks a bit strange and counterintuitive to me:

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

jq -n '1,2 | select(null)' outputs nothing

jq -n '1,2 | select(empty)' outputs nothing

jq -n '1,2 | select(42)' outputs 1 2

jq -n '1,2 | select(-1.23)' outputs 1 2

jq -n '1,2 | select({a:"strange"})' outputs 1 2

jq -n '1,0,-1,null,false,42 | select(.)' outputs 1 0 -1 42

It seems to me that everything that is not false and not null is considered true.

In the examples, the constants are to understand as placeholders for the result of an arbitrary expression.

>Solution :

Yes, null and false are indeed considered falsy, other values as truthy. This notion is (somewhat unfortunately) explained in the if-then-else section of the manual.

Therefore jq -n '1,2 | select(null)' will produce nothing, as would jq -n '1,2 | select(false)'

In the case of jq -n '1,2 | select(empty)', the empty just eats up all the results, so there is nothing to output.

All other cases are truthy, therefore the input is propagated.

Note that none of your examples considers the actual input for evaluation. All selects have a constant argument.

To filter based on the input, the argument of select has to somehow process it (as opposed to constants which simply ignore it), e.g. jq -n '1,2 | select(.%2 == 0)' outputs just 2.

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