1

I am trying to match a JSON array with a predefined expected json. The problem is that one of the key values in actual JSON is a set of strings delimited by "|". Here is how it looks :

actualJSON =  [
      {
        "a": "varA",
        "mix: "X|Y|Z",
       
      },
      {
        "b": "B",
        "c": "C"
      } ]

expectedJSON = [ 
      {
        "a": "varA",
        "mix: "Y|Z|X",
       
      },
      {
        "b": "B",
        "c": "C"
      } ]

Issue here is the mix key that represents a set of strings and the value can be any combination of "X|Y|Z" without any specific order like "Z|Y|X" etc. When the value of mix is Y|Z|X then

* match actualJSON contains expectedJSON 

works fine but in other cases it fails as expected. Is there a way to do the matching when key value is dynamic?

1 Answers1

0

My first suggestion is as far as possible to write tests where the response is 100% predictable and don't waste time on these weird cases. Also refer: https://stackoverflow.com/a/50350442/143475

That said this is easy to do if you write a JS function:

* def response = { foo: 'X|Y|Z' }
* def hasXyz = function(x){ return x.includes('X') && x.includes('Y') && x.includes('Z') }
* match response == { foo: '#? hasXyz(_)' }

I leave it to you to figure out better logic if you want. Refer: https://github.com/karatelabs/karate#self-validation-expressions

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248