Json使用jq来解析,读取,如果找到条目则返回true
抱歉,如果这是基本的,但 jq 的文档不太好 我有这个 json:
{
"jsonrpc": "2.0",
"result": [{
"hostid": "10084",
"host": "Zabbix server",
"interfaces": [{
"interfaceid": "1",
"ip": "127.0.0.1"
}]
}, {
"hostid": "10336",
"host": "AUTO",
"interfaces": [{
"interfaceid": "4",
"ip": "1.2.3.4"
}]
}, {
"hostid": "10337",
"host": "AUTOSERVER",
"interfaces": [{
"interfaceid": "5",
"ip": "4.5.6.7"
}]
}, {
"hostid": "10348",
"host": "Server00001",
"interfaces": [{
"interfaceid": "16",
"ip": "4.5.6.7"
}]
}],
"id": 2
}
我需要找到一种方法来使用 jq 来查找其中一台主机中是否存在“Server0001” 我知道我可以使用 grep 但我更喜欢在这里使用 jq,比如 select.. 任何帮助或参考好的文档将不胜感激
Apologies if this is basic but the doc for jq is not so good
i have this json:
{
"jsonrpc": "2.0",
"result": [{
"hostid": "10084",
"host": "Zabbix server",
"interfaces": [{
"interfaceid": "1",
"ip": "127.0.0.1"
}]
}, {
"hostid": "10336",
"host": "AUTO",
"interfaces": [{
"interfaceid": "4",
"ip": "1.2.3.4"
}]
}, {
"hostid": "10337",
"host": "AUTOSERVER",
"interfaces": [{
"interfaceid": "5",
"ip": "4.5.6.7"
}]
}, {
"hostid": "10348",
"host": "Server00001",
"interfaces": [{
"interfaceid": "16",
"ip": "4.5.6.7"
}]
}],
"id": 2
}
i need to find a way to use jq to find if "Server0001" exists in one of the hosts
i know i can use grep but i prefer using jq here, like select..
any help or ref toa good doc would be much appriciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何
(请参阅manual) 如果条件与至少一项匹配,则可以返回布尔值。演示
演示
您可能还想在调用
jq
时使用一些参数(请参阅manual):--arg
选项,例如,允许您添加一个可以从过滤器字符串外部初始化的变量。使用-e
(或--exit-status
)标志,您可以让jq
根据过滤器的最终结果设置退出状态。总之,这使您能够像这样使用jq
:any
(see the manual) can return a boolean value if a condition matches with at least one item.Demo
Demo
You may also want to use the some parameters when invoking
jq
(see the manual): The--arg
option, for instance, lets you add a variable which can be initialized from outside the filter string. And with the-e
(or--exit-status
) flag you can havejq
set the exit status according to the filter's final result. Together, this enables you to usejq
like this: