1

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.

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Note that on this topic (the alternative operator), the 1.6 documentation is identical to the 1.5 one.
@peak: At first I though the documentation for 1.5 and 1.6 ist still right but so "packed" that mere mortals cannot unpack it. After more thinking about the case "left side produces more values and only some of them are truish" it think the documentation is wrong indeed. But the sentence of the FAQ is genious: Short, exact and -- above all -- understandable.
1

Exactly after posting my question I stumbled over the exact answer in the manual of the developer version. :shrug:

So far the manual page for jq 1.5 says about //:

A filter of the form a // b produces the same results as a, if a produces results other than false and null. Otherwise, a // b produces the same results as b.

The developer version says additionally:

[...]Note: some_generator // defaults_here is not the same as some_generator | . // defaults_here. The latter will produce default values for all non-false, non-null values of the left-hand side, while the former will not. [...] In (false, null, 1) | . // 42 the left-hand side of // is ., which always produces just one value, while in (false, null, 1) // 42 the left-hand side is a generator of three values, and since it produces a value other false and null, the default 42 is not produced.

Comments

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.