简体   繁体   English

如何使用 jq 在 json 中的数组中 select 多个值

[英]How to select multiple values in an array in json using jq

示例 json Am using jq to get multiple responses from the JSON file using the below command.我正在使用 jq 使用以下命令从 JSON 文件中获取多个响应。

.components| to_entries[]| "\(.key)- \(.value.status)"

which gives me below这给了我下面

Server2- UP服务器 2-UP

server1 - UP服务器 1 - 已启动

Splunk- UP Splunk-UP

Datameer - UP Datameer - UP

Platfora - UP平台 - UP

diskSpace- Good磁盘空间-好

But I want to select only a few I tried giving in braces of to_entries[] but it didn't work.但是我想 select 只尝试了几个我尝试给 to_entries[] 大括号但它没有用。

Expected output:预期 output:

Server1 - UP服务器 1 - 已启动

Splunk -UP Splunk-UP

Platfora - UP平台 - UP

Is there any way to pick only a few values.有没有办法只选择几个值。 Appreciate your help.感谢你的帮助。 Thank you.谢谢你。

With the -r command-line option, the following transforms the given input to the desired output, and is perhaps close to what you're looking for:使用 -r 命令行选项,以下将给定输入转换为所需的 output,并且可能接近您正在寻找的内容:

.components
| to_entries[]
| select(.key == ("server1", "Splunk", "Platfora"))
| "\(.key)- \(.value.status)"

If the list of components is available as a JSON list, then you could modify the selection criterion accordingly, eg using IN (uppercase) or index .如果组件列表以 JSON 列表的形式提供,那么您可以相应地修改选择标准,例如使用IN (大写)或index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM