'How to check a particular value on basis of condition in karate

Goal: Match the check value is correct for 123S and 123O response in API

First check the value on this location x.details[0].user.school.name[0].codeable.text if it is 123S then check if x.details[0].data.check value is abc

Then check if the value on this location x.details[1].user.school.name[0].codeable.text is 123O then check if x.details[1].data.check is xyz

The response in array inter changes it is not mandatory first element is 123S sometime API returns 123O as first array response.

Sample JSON.

{
    "type": "1",
    "array": 2,
    "details": [
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "yty",
                                        "condition": "check1"
                                    }
                                ],
                                "text": "123S"
                            }
                        }
                    ]
                },
                "sample": "test1",
                "id": "22222"
            },
            "data": {
                "check": "abc"
            }
        },
        {
            "path": "path",
            "user": {
                "school": {
                    "name": [
                        {
                            "value": "this is school",
                            "codeable": {
                                "details": [
                                    {
                                        "hello": "def",
                                        "condition": "check2"
                                    }
                                ],
                                "text": "123O"
                            }
                        }
                    ]
                },
                "sample": "test",
                "id": "11111"
            },
            "data": {
                "check": "xyz"
            }
        }
    ]
}

How I did in Postman but how to replicate same in Karate?

var jsonData = pm.response.json();

pm.test("Body matches string", function () {
    for(var i=0;i<jsonData.details.length;i++){
        if(jsonData.details[i].user.school.name[0].codeable.text == '123S')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('abc');
        }
        if(jsonData.details[i].user.school.name[0].codeable.text == '123O')
        {
            pm.expect(jsonData.details[i].data.check).to.equal('xyz');
        }
    }
});


Solution 1:[1]

2 lines. And this takes care of any number of combinations of lookup values :)

* def lookup = { '123S': 'abc', '123O': 'xyz' }
* match each response.details contains { data: { check: '#(lookup[_$.user.school.name[0].codeable.text])' } }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas