【问题标题】:Filter azure cli output based on nested array values with jmespath使用 jmespath 根据嵌套数组值过滤 azure cli 输出
【发布时间】:2022-01-16 03:26:38
【问题描述】:

我正在尝试根据 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
    }
]

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

  • az pipelines release definition list --artifact-type build --query "[artifacts[?ends_with(alias, '_suffix')]]"
    
  • az pipelines release definition list --artifact-type build --query "[].[artifacts[?ends_with(alias, '_suffix')]]"
    
  • az pipelines release definition list --artifact-type build --query "[].artifacts[?ends_with(alias, '_suffix')]"
    

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

【问题讨论】:

  • 您期望结果是什么? [].artifacts[?ends_with(alias, '_suffix')] 是正确的语法。
  • 我希望获得完整的输出列表,而不仅仅是工件部分。我想要 artifacts.alias 中包含 _suffix 的所有内容。
  • 那么您必须提供足够的数据(输出),以便我们可以重现您的问题并为您提供线索,正如目前所写的那样,您已经有了解决方案。
  • 我在问题中添加了一个更好的输出示例和一个预期输出。

标签: azure-cli jmespath


【解决方案1】:

为此,您必须有一个 "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
  }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多