使用 jq 获取数组中的键等于特定值的数组?

发布于 2025-01-11 15:01:50 字数 771 浏览 0 评论 0原文

我一直在练习 jq play 来尝试获取列表中的所有数组,其中 website 是 ==“google”,并从中创建另一个 json 列表。

https://jqplay.org/s/DKNC2mhOLq

jq:错误(位于:18):无法索引包含字符串“website”的数组 exit status 5

{
    "items": [
    {
     "name":"name1",
     "id":"1",
     "website":"google"
    },
    {
     "name":"name1",
     "id":"1",
     "website":"google"
    },
    {
     "name":"name1",
     "id":"2",
     "website":"jingle"
    }
    ]

所需的输出:

   [
    {
     "name":"name1",
     "id":"1",
     "website":"google"
     },
    {
     "name":"name1",
     "id":"1",
     "website":"google"
     }
   ]

如何循环遍历列表中的数组并查找特定键的特定值?感谢您提供的任何帮助或想法。我是 JSON 和 jq 的初学者。

I have been practicing with jq play to try to get all the arrays in a list where website is == "google" and create another json list from that.

https://jqplay.org/s/DKNC2mhOLq

jq: error (at :18): Cannot index array with string "website"
exit status 5

{
    "items": [
    {
     "name":"name1",
     "id":"1",
     "website":"google"
    },
    {
     "name":"name1",
     "id":"1",
     "website":"google"
    },
    {
     "name":"name1",
     "id":"2",
     "website":"jingle"
    }
    ]

Desired output:

   [
    {
     "name":"name1",
     "id":"1",
     "website":"google"
     },
    {
     "name":"name1",
     "id":"1",
     "website":"google"
     }
   ]

how can I loop through arrays in a list and look for specific values for specific keys? Thanks for any help or ideas you can provide. I am a begginer with JSON and jq.

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

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

发布评论

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

评论(1

三岁铭 2025-01-18 15:01:50

selectmap 括起来,因为您希望将过滤器单独应用于每个数组项,同时保留周围的数组结构。

jq '.items | map(select(.website == "google"))'
[
  {
    "name": "name1",
    "id": "1",
    "website": "google"
  },
  {
    "name": "name1",
    "id": "1",
    "website": "google"
  }
]

演示

Enclose the select with a map, as you want to apply the filter to each array item individually while retaining the surrounding array structure.

jq '.items | map(select(.website == "google"))'
[
  {
    "name": "name1",
    "id": "1",
    "website": "google"
  },
  {
    "name": "name1",
    "id": "1",
    "website": "google"
  }
]

Demo

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