json jq 条件输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19573517/
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 19:57:58  来源:igfitidea点击:

jq Conditional output

jsoncommand-linejq

提问by clwen

I'm using jqto play with json. I was wondering how to conditionally print something in that.

我正在使用jq来玩 json。我想知道如何有条件地打印一些东西。

Say I am interested in a field call geo. I used the following command and find out there is only one entry whose geois null:

说我对实地考察感兴趣geo。我使用了以下命令,发现只有一个条目geonull

% cat all.json | jq '.geo != null' | sort | uniq -c              
   1 false
6891 true

How can I print out that entry only without printing everything else?

如何仅打印该条目而不打印其他所有内容?

Didn't see something like printcommand in the manual. And this doesn't work: cat all.json | jq 'if .place == null then . end'. jqcomplained about syntax error.

print在手册中没有看到类似命令的内容。这不起作用:cat all.json | jq 'if .place == null then . end'. jq抱怨语法错误。

回答by max taldykin

You can use the selectfunction to get only required entries:

您可以使用select函数仅获取必需的条目:

jq 'select(.geo != null)' all.json