Search code examples
jmespath

Jmespath setting the jmespath expr dynamically


I am using jmespath as follows

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",
        },
    ]
}

And setting the jmespath expr dynamically as:

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

Instead of getting the dictionary corresponding to "SalesMarketing3", I am getting output as follows, Am I missing something?

'value[?name == SalesMarketing3]'

Any help is highly appreciated.


Solution

  • Your issue is coming from the fact that you have to search on the string or raw string literal SalesMarketing3.
    In JMESPath:

    • A string is delimited by single quotes: 'SalesMarketing3'
    • A raw string literal is delimited by backticks: `SalesMarketing3`

    Given 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))
    

    This gives the expected output:

    [
      {
        "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"
      }
    ]