if-then-else): if A then B end is the same as if A then B else . end. That is, the els-6ren">
gpt4 book ai didi

json - 为什么 jq 1.6 不接受这个 "if A then B end"语句?

转载 作者:行者123 更新时间:2023-12-02 02:58:37 30 4
gpt4 key购买 nike

根据jq手册(条件和比较> 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 ..

这个 Stack Overflow 问题的公认答案也证实了这一点: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"

最佳答案

关于json - 为什么 jq 1.6 不接受这个 "if A then B end"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60517702/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com