如何利用JQ过滤量超过0的条目?

发布于 2025-02-12 20:26:09 字数 928 浏览 0 评论 0原文

我有一个gitHub命令行调用的结果,其中包括以下JSON格式:

[
  {
    "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列表的输出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

I have the results of a Github Commandline call with the following JSON format:

[
  {
    "name": "repository-1",
    "pullRequests": {
      "totalCount": 129
    }
  },
  {
    "name": "repository-2",
    "pullRequests": {
      "totalCount": 1143
    }
  },
  {
    "name": "repository-3",
    "pullRequests": {
      "totalCount": 78
    }
  },
  {
    "name": "repository-4",
    "pullRequests": {
      "totalCount": 0
    }
  }
]

This is the output of gh repo list ORG-REPO --json pullRequests,name

I have many many more repositories in there and my goal is to get a list of all repositories that have 0 pull requests so that I can act on those (archive).

How can I do this utilizing jq?

I've tried:

.[].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

How can I filter the list of repositories to only show the repository name where pullRequests.totalCount = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2025-02-19 20:26:09

使用选择以按照标准过滤,此处.pullrequests.totalcount == 0map将输入阵列:

jq 'map(select(.pullRequests.totalCount == 0))'
[
  {
    "name": "repository-4",
    "pullRequests": {
      "totalCount": 0
    }
  }
]

如果您只对名称,将其添加到映射中:

jq 'map(select(.pullRequests.totalCount == 0).name)'
[
  "repository-4"
]

demo

如果您想要名称为newline-列表 -分开的原始文本而不是json字符串数组,在元素上迭代并使用-raw-of-oftput-r用于输出:

jq -r '.[] | select(.pullRequests.totalCount == 0).name'
repository-4

demo

Use select to filter by criteria, here .pullRequests.totalCount == 0, and map that onto the input array:

jq 'map(select(.pullRequests.totalCount == 0))'
[
  {
    "name": "repository-4",
    "pullRequests": {
      "totalCount": 0
    }
  }
]

Demo

If you are only interested in the names, add that to the mapping:

jq 'map(select(.pullRequests.totalCount == 0).name)'
[
  "repository-4"
]

Demo

If you want the names as a list of newline-separated raw texts rather than a JSON array of strings, iterate over the elements and use the --raw-output or -r for the output:

jq -r '.[] | select(.pullRequests.totalCount == 0).name'
repository-4

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文