gpt4 book ai didi

arrays - 使用jq根据对象中的键值从Array返回整个对象

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:10 25 4
gpt4 key购买 nike

我有一个 .json 文件,如果该对象中的键值等于“失败”,我想通过 jq 过滤以返回数组中的整个对象。我该怎么做?

示例 json:

"version": "0.26.0",
"controls": [{
"id": "os-1.0",
"status": "passed",
"code_desc": "File /etc/profile content should match /umask\\s*022/",
"profile_id": "test"
}, {
"id": "os-1.0",
"status": "passed",
"code_desc": "File /etc/bashrc content should match /umask\\s*022/",
"profile_id": "test"
}, {
"id": "os-1.0",
"status": "failed",
"code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
"profile_id": "test"
"message": "\nexpected: \"/sbin/sulogin\"\n got: \n\n(compared using `cmp` matcher)\n"
}]

输出到新文件:

{
"id": "os-1.0",
"status": "failed",
"code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
"profile_id": "test"
"message": "\nexpected: \"/sbin/sulogin\"\n got: \n\n(compared using `cmp` matcher)\n"
}

最佳答案

显示为 JSON 的文本作为 JSON 无效。假设它是固定的结构{"version": _, "controls": _},下面的过滤器将产生如下所示的结果:

.controls[] | select(.status == "failed")

输出:

{
"id": "os-1.0",
"status": "failed",
"code_desc": "File /etc/csh.cshrc content should match /umask\\s*022/",
"profile_id": "test",
"message": "\nexpected: \"/sbin/sulogin\"\n got: \n\n(compared using `cmp` matcher)\n"
}

注意:为了稳健性,您可能希望使用 .status? 而不是 .status

关于arrays - 使用jq根据对象中的键值从Array返回整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38357075/

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