Home > OS >  Karate : Matching data between nested array
Karate : Matching data between nested array

Time:08-09

Is there a way to match the response data from API which contain a nested array for a key where key-value pair are in different order inside the nested array in karate?

Scenario: Verify original data contains expected data

  • def original = [{ a:1, b: [{c:2},{d:3}]}]
  • def expected = [{ b: [{d:3},{c:2}], a:1 }]

Using contains deep method will solve the issue but I am expecting original data from a API response so in some point of time if one more field gets added to the API response, then my scenario will still get passed

CodePudding user response:

Don't try to do everything in one-line. Split your matches, and there is more explanation in the docs:

* def inner =  [{ c: 2 }, { d: 3 }]
* def response = [{ a: 1, b: [{ d: 3 }, { c: 2 }]}]

* match each response contains { b: '#(^^inner)' }
* match each response == { a: 1, b: '#(^^inner)' }
* match response[0] == { a: 1, b: '#(^^inner)' }
* match response == [{ a: 1, b: '#(^^inner)' }]

You don't need to use all of these, I'm showing the possible options.

  • Related