JSON JQ 如果没有其他

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29949184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:52:44  来源:igfitidea点击:

JSON JQ if without else

jsonif-statementjq

提问by user1578872

I use the following JQ command to filter out the JSON. My requirement is to filter out the JSON message if the expected node is present. Or else, do nothing. Hence, I use if, elif, ....

我使用以下 JQ 命令过滤掉 JSON。我的要求是在预期节点存在时过滤掉 JSON 消息。否则,什么都不做。因此,我使用 if, elif, ....

sed -n "s/.*Service - //p" /response.log* |
  jq "if (.requests | length) != 0 then .requests |= map(select(.id == \"123\"))
      elif (.result | length ) != 0  then .result |= map(select(.id== \"123\")) 
      else " "  end" > ~/result.log

Looks like else is mandatory here. I dont want to do anything inside the else block. Is there anyway, I can ignore else or just print some whitespce inside else.

看起来 else 在这里是强制性的。我不想在 else 块内做任何事情。无论如何,我可以忽略 else 或只是在 else 中打印一些 whitespce 。

In the above case, it prints double quotes " " in the result file.

在上述情况下,它在结果文件中打印双引号“”。

回答by peak

You may want to use the idiom:

你可能想使用成语:

if CONDITION then WHATEVER else empty end

emptyis a filter that outputs nothing at all -- not even null, which is after all something (namely a JSON value). It's a bit like a black hole, only blacker -- it will consume whatever it's offered, but unlike a black hole, it does not even emit Hawking radiation.

empty是一个根本不输出任何内容的过滤器——甚至不输出 null,毕竟这是一些东西(即 JSON 值)。它有点像黑洞,只是更黑——它会消耗它提供的任何东西,但与黑洞不同的是,它甚至不发射霍金辐射。

In your case, you have an "elif" so using "else empty" is probably what you want, but for reference, the above is exactly equivalent to:

在您的情况下,您有一个“elif”,因此使用“else empty”可能是您想要的,但作为参考,以上完全等同于:

select(CONDITION) | WHATEVER

P.S. My guess is that whatever the goal of the sed command, it could be done more reliably as part of the jq program, perhaps using walk/1.

PS 我的猜测是,无论 sed 命令的目标是什么,它都可以作为 jq 程序的一部分更可靠地完成,也许使用walk/1.

UPDATE

更新

After the release of jq 1.6, a change was made so that "if without else" has the semantics of "if _ then _ else . end", that is:

jq 1.6 发布后,做了一个改动,使得“if without else”的语义为“if _ then _ else .end”,即:

if P then Q end=== if P then Q else . end

if P then Q end=== if P then Q else . end

回答by Jeff Mercado

Since you're not making any further changes to the object, just use the "identity" filter ..

由于您没有对对象进行任何进一步的更改,因此只需使用“身份”过滤器.

if (.requests | length) then ... else . end

On the other hand, you're just updating the requestsor resultproperty if non-empty using map. The check isn't necessary. If it's empty, then it will return empty.

在另一方面,你只是在更新requestsresult在非空时使用属性map。检查是没有必要的。如果它是空的,那么它将返回空。

You can simplify your filter to just:

您可以将过滤器简化为:

.requests |= map(select(.id == \"123\")) | .result |= map(select(.id== \"123\"))