Search code examples
jqsemanticstruthy

Strange behaviour of the alternative operator //


Using JQ 1.5 I've problems understanding the output of the alternative operator // in a specific situation.

Given this input:

{
    "a": 42,
    "b": false,
    "c": null
}

I expected the expression (.a, .b, .c, .d) // -1 to return this:

42
-1
-1
-1

But in fact it returns this instead:

42

If I replace // with another operator like < then I indeed get four results instead of just one (the actual results are not important here, just their number):

> jq '(.a, .b, .c, .d) < -1' input.json
false
true
true
true

Note: The expression (.a, .b, .c, .d) | . // -1 returns the expected output. This is not the question. I'd like to know why the initial expression does not work.


Solution

  • The 1.5 documentation is, to put it bluntly, just wrong.

    The jq FAQ provides this succinct and useful summary:

    "A // B" either produces the truthy elements of A if there are any,
    or else the entire stream B.