【问题标题】:jq filter query based on inner array itemsjq根据内部数组项过滤查询
【发布时间】:2017-07-23 09:45:20
【问题描述】:

我有一个json作为

[
  {
    "id":1,
    "author": "hippy",
    "reviewers": [
      {
        "name": "hippy",
        "status": "ok"
      },
      {
        "name": "other",
        "status": "ok"
      }
    ]
  },
  {
    "id":2,
    "author": "hippy",
    "reviewers": [
      {
        "name": "hippy",
        "status": "ok"
      },
      {
        "name": "build",
        "status": "ok"
      }
    ]
  },
  {
    "id":3,
    "author": "hippy",
    "reviewers": [
      {
        "name": "hippy",
        "status": "ok"
      }
    ]
  },
  {
    "id":4,
    "author": "other",
    "reviewers": [
      {
        "name": "hippy",
        "status": "ok"
      }
    ]
  }
]

我想在排除审阅者build后得到reviewerauthor相同的项目。

即我想用ids 2, 3 获取项目。

我能做到这么远

.[] 
| select(
    .author as $author 
    | {reviewers} 
    | .[] 
    | map(.name) 
    | select(.[] == $author)
    )

但是带有id 1 的项目是误报,我也想过滤掉它。

【问题讨论】:

    标签: json select jq


    【解决方案1】:

    从描述和您只想要这两个项目的事实来看,我相信这就是您要寻找的:

    .[]
    | select( .author as $author
              | .reviewers
              | map(select(.name != "build"))        # ignore "build"
              | length==1 and .[0].name == $author )
    

    【讨论】:

      猜你喜欢
      • 2014-12-29
      • 2021-12-25
      • 2021-06-23
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多