2

According to the jq manual (Conditionals and Comparisons > if-then-else):

if A then B end is the same as if 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"
StoneThrow
  • 5,314
  • 4
  • 44
  • 86

1 Answers1

5

I think you're looking at the manual for the development version of jq, rather than the manual for jq version 1.6.

user197693
  • 1,935
  • 10
  • 8