我有一个使用以下JSON格式的Github命令行调用的结果:
[
{
"name": "repository-1",
"pullRequests": {
"totalCount": 129
}
},
{
"name": "repository-2",
"pullRequests": {
"totalCount": 1143
}
},
{
"name": "repository-3",
"pullRequests": {
"totalCount": 78
}
},
{
"name": "repository-4",
"pullRequests": {
"totalCount": 0
}
}
]这是gh repo list ORG-REPO --json pullRequests,name的输出
我有很多更多的存储库,我的目标是获得所有拥有0拉请求的存储库的列表,这样我就可以对那些(归档)操作。
我如何利用jq来做这件事?
我试过:
.[].pullRequests.totalCount -> This gives me the numeric value of each record
.[].pullRequests.totalCount,.[].name -> This gives me the list from above and then the list of repository names如何筛选存储库列表,使其仅显示存储库名称,其中pullRequests.totalCount = 0
发布于 2022-07-01 21:41:17
使用select按条件进行筛选,这里是输入数组上的.pullRequests.totalCount == 0和map:
jq 'map(select(.pullRequests.totalCount == 0))'[
{
"name": "repository-4",
"pullRequests": {
"totalCount": 0
}
}
]如果您只对名称感兴趣,请将其添加到映射中:
jq 'map(select(.pullRequests.totalCount == 0).name)'[
"repository-4"
]如果要将名称作为换行符分隔的原始文本的列表,而不是字符串的JSON数组,则在元素上迭代,并使用--raw-output或-r作为输出:
jq -r '.[] | select(.pullRequests.totalCount == 0).name'repository-4https://stackoverflow.com/questions/72834805
复制相似问题