使用 jmespath 根据嵌套数组值过滤 azure cli 输出

Filter azure cli output based on nested array values with jmespath

我正在尝试根据 artifacts[].alias 值过滤以下 Azure CLI 命令的输出。

命令

az pipelines release definition list --artifact-type build

输出(简体)

[
    {
        "artifacts": [
            {
                "alias": "alias_with_suffix",
                "definitionReference": {
                    "IsMultiDefinitionType": {
                        "id": "False",
                        "name": "False"
                    }
                },
                "type": "Build"
            }
        ],
        "createdOn": "2021-06-16T15:15:07.620000+00:00",
        "id": 88,
        "isDeleted": false,
        "modifiedOn": "2021-09-15T10:39:14.257000+00:00",
        "revision": 5
    },
    {
        "artifacts": [
            {
                "alias": "alias",
                "definitionReference": {
                    "IsMultiDefinitionType": {
                        "id": "False",
                        "name": "False"
                    }
                },
                "type": "Build"
            }
        ],
        "createdOn": "2021-06-16T15:15:07.620000+00:00",
        "id": 88,
        "isDeleted": false,
        "modifiedOn": "2021-09-15T10:39:14.257000+00:00",
        "revision": 5
    },
    {
        "artifacts": null,
        "createdOn": "2021-06-16T15:15:07.620000+00:00",
        "id": 88,
        "isDeleted": false,
        "modifiedOn": "2021-09-15T10:39:14.257000+00:00",
        "revision": 5
    }
]

预期输出

[
    {
        "artifacts": [
            {
                "alias": "alias_with_suffix",
                "definitionReference": {
                    "IsMultiDefinitionType": {
                        "id": "False",
                        "name": "False"
                    }
                },
                "type": "Build"
            }
        ],
        "createdOn": "2021-06-16T15:15:07.620000+00:00",
        "id": 88,
        "isDeleted": false,
        "modifiedOn": "2021-09-15T10:39:14.257000+00:00",
        "revision": 5
    }
]

到目前为止我已经尝试了以下方法:

我也尝试过使用 [*]..... 之类的东西或在 [] | []..... 之类的命令之间使用管道结果(有或没有 *),但我总是得到 [null] 或一些输出 null 与有效值混合的列表。

为此,您必须有一个 "nested" 过滤器表达式,因为您对 "parent" 对象感兴趣基于键 artifacts 数组中的条件为真这一事实。

因此,给定查询:

[?artifacts[?ends_with(alias, '_suffix')]]

你最终得到了,你提供的 JSON 作为输入,结果是 JSON:

[
  {
    "artifacts": [
      {
        "alias": "alias_with_suffix",
        "definitionReference": {
          "IsMultiDefinitionType": {
            "id": "False",
            "name": "False"
          }
        },
        "type": "Build"
      }
    ],
    "createdOn": "2021-06-16T15:15:07.620000+00:00",
    "id": 88,
    "isDeleted": false,
    "modifiedOn": "2021-09-15T10:39:14.257000+00:00",
    "revision": 5
  }
]