为什么jq 1.6不接受此“ if A then B end”语句?

时间:2020-03-04 01:05:46

标签: json jq

根据jq手册(“条件和比较> if-then-else”):

if A then B endif A then B else . end相同。那就是 else分支是可选的,如果不存在则与..

相同

对于此堆栈溢出问题的公认答案也证实了这一点:JSON JQ if without else

那么为什么这个if A then B end语句为什么会调用解析错误?

$ 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

我理解为语句的等效 if A then B else . end形式似乎有用:

$ echo 2 | jq 'if . == 0 then "zero" else . end'
2
$ echo 0 | jq 'if . == 0 then "zero" else . end'
"zero"

1 个答案:

答案 0 :(得分:4)

我认为您正在使用的是manual for the development version of jq,而不是the manual for jq version 1.6