Is there way/possible to use IN operator in JMESPath

54 views Asked by At

I'm trying to compare the attributes of a JSON object based on condition. I.e.:

if is_encrypted = YES encryption.state == 'encrypted' or a[0].name in encrypted_list else NO

encrypted_list contains:

{
    "encrypted_list": [
        "ggr_test",
        "ggr_test1",
        "ggr1"
    ]
}

Facts output is:

{
    "facts": {
        "changed": true,
        "failed": false,
        "response": {
            "num_records": 2,
            "records": [
                {
                    "a": [
                        {
                            "name": "ggr1",
                            "uuid": "xyze"
                        }
                    ],
                    "encryption": {
                        "enabled": false,
                        "key_id": "",
                        "state": "unencrypted",
                        "type": "none"
                    },
                    "name": "abc_root",
                    "uuid": "xyz"
                },
                {
                    "a": [
                        {
                            "name": "ggr_test",
                            "uuid": "xyz1"
                        }
                    ],
                    "encryption": {
                        "enabled": false,
                        "key_id": "",
                        "state": "unencrypted",
                        "type": "none"
                    },
                    "name": "abc_test",
                    "uuid": "xyzf"
                }
            ]
        },
        "status_code": 200
    }
}

My expected output would be:

[
    {
        "is_encrypted": "YES",
        "name": "abc_root"
    },
    {
        "is_encrypted": "YES",
        "name": "abc_test"
    }
]

I'm trying below format with json_query

encryption_info: "{{ encryption_info | default([]) + facts | json_query('response.records[*].{name:name, is_encrypted: 'YES' if(encryption.state == 'encrypted' || a[0].name in encrypted_list) else 'NO'}') }}"

But getting below error:

FAILED! => {"msg": "template error while templating string: expected token ',', got 'YES'. String:

Am I missing something?

0

There are 0 answers