Search code examples
karateassertion

AND condition in Karate


[ { “X”: “shop”, "description": “ABC”, “Y”: { “Z”: { “T1”: “1”, “T2”: “2”} } }]

Now need to write a condition in karate that if (T1 == ‘1’ && T2 == ‘2’) then assert that “X” = “shop” How can we achieve this in a single line or in short way

Tried various methods but did not work


Solution

  • This is certainly very complicated JSON, so I would try to "pre process" it to be simpler.

    Here is one possible solution out of many. Please use the hints to arrive at what works for you:

    * def response = [{ X: 'shop', Y: { Z: { T1: '1', T2: '2' } } }]
    * def data = response.map(x => ({ X: x.X, T1: x.Y.Z.T1, T2: x.Y.Z.T2 }))
    * def good = data.filter(x => x.T1 == '1' && x.T2 == '2' && x.X == 'shop')
    * match data == good
    

    Refer here for more hints: https://stackoverflow.com/a/62567262/143475