According to the jq
manual (Conditionals and Comparisons > if-then-else):
if A then B end
is the same asif A then B else . end
. That is, the else branch is optional, and if absent is the same as ..
The same is substantiated by the accepted answer to this Stack Overflow question: JSON JQ if without else
So why does this if A then B end
statement invoke a parse error?
$ jq --version
jq-1.6
$ echo 2 | jq 'if . == 0 then "zero" end'
jq: error: syntax error, unexpected end (Unix shell quoting issues?) at <top-level>, line 1:
if . == 0 then "zero" end
jq: error: Possibly unterminated 'if' statement at <top-level>, line 1:
if . == 0 then "zero" end
jq: 2 compile errors
$ echo 0 | jq 'if . == 0 then "zero" end'
jq: error: syntax error, unexpected end (Unix shell quoting issues?) at <top-level>, line 1:
if . == 0 then "zero" end
jq: error: Possibly unterminated 'if' statement at <top-level>, line 1:
if . == 0 then "zero" end
jq: 2 compile errors
What I understand to be the equivalent if A then B else . end
form of the statement seems to work:
$ echo 2 | jq 'if . == 0 then "zero" else . end'
2
$ echo 0 | jq 'if . == 0 then "zero" else . end'
"zero"