基于内部数组项的jq过滤器查询

时间:2017-07-23 09:45:20

标签: json select jq

我有一个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"
      }
    ]
  }
]

我想在排除评论者reviewer后获取authorbuild相同的项目。

即。我希望获得id s 2,3的项目。

我能够到目前为止

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

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

1 个答案:

答案 0 :(得分:2)

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

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