首页 > 解决方案 > 使用 jmespath 过滤嵌套数组(使用 az cli)

问题描述

我想知道 TXT 记录中的值是否已在 DNS 中可用:

az network dns record-set txt show -g myresourcegroup -z 'mydomain' -n 'mytxtvalues'

这是 json 结果的一部分:

 "txtRecords": [
    {
      "value": [
        "abc"
      ]
    },
    {
      "value": [
        "def"
      ]
    }
  ]

我尝试了很多查询。这些是我希望工作的 2 个。

az network dns record-set txt show -g myresourcegroup -z 'mydomain.com' -n 'mytxtvalues' --query txtRecords[?value[?starts_with(@, 'abc')]]

az network dns record-set txt show -g myresourcegroup -z 'mydomain.com' -n 'mytxtvalues' --query txtRecords[*].value[?starts_with(@, 'abc')]]

结果是:

在 line:1 char:123 + ... 'mytxtvalues' --query txtRecords[?value[?starts_with(@, 'abc') ... + ~ 源文本中无法识别的标记。在 line:1 char:124 + ... 'mytxtvalues' --query txtRecords[?value[?starts_with(@, 'abc')] ... + ~ 参数列表中缺少参数。

看起来用于过滤数组的 @ 无法识别。但我不知道如何查询。

知道值 abc 是否已经在列表中的正确查询是什么?

标签: azure-clijmespath

解决方案


您需要使用如下命令:

az network dns record-set txt show -g myresourcegroup -z 'mydomain.com' -n 'mytxtvalues' --query "txtRecords[*].value[?starts_with(@, 'abc')]"

如果您只需要输出字符串,您可以附加参数-o tsv。希望这会帮助你。


推荐阅读