Home > OS >  Validate each item in a json list is contained in another json list
Validate each item in a json list is contained in another json list

Time:04-13

* def list1 = [{'a':'value'}, {'b':'value2'}, {'c':'value4'}]
* def list2 = [{'a':'value'}, {'b':'value2'}]

I want to validate if all items in list2 exist in list1 using Karate I am getting boths lists as response from different APIs. Some child values in the given example contain json objects.

I tried to solve this by creating a function and maintaining an array of validation successes(Not optimal). But that too failed as the json were compared as strings and child json objects inside the values mentioned in the example above had different structure(Some contained spaces between parenthesis and newline characters).

def result = []
def iterateJson = 
'''
function(item){
if(karate.get('payload').contains(item)
   karate.append(result, true)
else
   karate.append(result, false)
}

'''
karate.foreach(response, iterateJson)
match each result[*] == true

CodePudding user response:

You seem to be over-thinking this !

* def list1 = [{'a':'value'}, {'b':'value2'}, {'c':'value4'}]
* def list2 = [{'a':'value'}, {'b':'value2'}]
* match list1 contains list2

And yes, child objects are handled. Try it and see.

  • Related