Jmespath 动态设置 jmespath expr

Jmespath setting the jmespath expr dynamically

我使用的jmespath如下

data = {
    "value": [
        {
            "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
            "id": "5b218778-e7a5-4d73-8187-f10824047715",
            "name": "SalesMarketing2",
            "webUrl": "https://app.powerbi.com/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/reports/5b218778-e7a5-4d73-8187-f10824047715",
            "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715&groupId=f089354e-8366-4e18-aea3-4cb4a3a50b48",
        },
        {
            "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
            "id": "5b218778-e7a5-4d73-8187-f10824047715",
            "name": "SalesMarketing3",
            "webUrl": "https://app.powerbi.com/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/reports/5b218778-e7a5-4d73-8187-f10824047715",
            "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715&groupId=f089354e-8366-4e18-aea3-4cb4a3a50b48",
        },
    ]
}

并将 jmespath expr 动态设置为:

report= 'SalesMarketing3'
jmespath.search(f"'value[?name == {report}]'", data)

我没有得到对应于“SalesMarketing3”的字典,而是得到如下输出,我是不是遗漏了什么?

'value[?name == SalesMarketing3]'

非常感谢任何帮助。

您的问题是因为您必须搜索 字符串 原始字符串文字 SalesMarketing3
在 JMESPath 中:

  • 字符串用单引号分隔:'SalesMarketing3'
  • 原始字符串文字由反引号分隔:`SalesMarketing3`

给定 example.py:

import jmespath

data = {
    "value": [
        {
            "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
            "id": "5b218778-e7a5-4d73-8187-f10824047715",
            "name": "SalesMarketing2",
            "webUrl": "https://app.powerbi.com/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/reports/5b218778-e7a5-4d73-8187-f10824047715",
            "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715&groupId=f089354e-8366-4e18-aea3-4cb4a3a50b48",
        },
        {
            "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
            "id": "5b218778-e7a5-4d73-8187-f10824047715",
            "name": "SalesMarketing3",
            "webUrl": "https://app.powerbi.com/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/reports/5b218778-e7a5-4d73-8187-f10824047715",
            "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715&groupId=f089354e-8366-4e18-aea3-4cb4a3a50b48",
        },
    ]
}
report = 'SalesMarketing3'

print(jmespath.search(f"value[?name == `{report}`]", data))
## Another equivalent is:
# print(jmespath.search(f"value[?name == '{report}']", data))

这给出了预期的输出:

[
  {
    "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229",
    "id": "5b218778-e7a5-4d73-8187-f10824047715",
    "name": "SalesMarketing3",
    "webUrl": "https://app.powerbi.com/groups/f089354e-8366-4e18-aea3-4cb4a3a50b48/reports/5b218778-e7a5-4d73-8187-f10824047715",
    "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=5b218778-e7a5-4d73-8187-f10824047715&groupId=f089354e-8366-4e18-aea3-4cb4a3a50b48"
  }
]

您可以使用 JMESPath 终端 以交互方式编写复杂查询 - 当您受困于 JMESPath 时,它非常有用。您可以在此处找到有关该工具的更多信息: https://www.doaws.pl/blog/2021-12-05-how-to-master-aws-cli-in-15-minutes/how-to-master-aws-cli-in-15-minutes