How can we ignore some nested fields while comparing two responses in karate, i am using "contains deep" for the comparison

70 Views Asked by At

I want to compare two nested json response, so i am using "contains deep". But while comparison i want to ingnore some fields(skip comparison on those fields).

Example: I want to ignore the state field while comparing both response given below.

Scenario1:
    * def response1 = { id: 1, name: 'John', address: { city: 'New York', state: 'NY' } }
    * def response2 = { id: 1, name: 'John', address: { city: 'California', state: 'CA' } }

    * match response1 contains deep response2

# Note: My requirement is tp show difference of city only as state i want to ignore in the karate report
Scenario2:
    * def response1 = { id: 1, name: 'John', address: { city: 'New York', state: 'NY' } }
    * def response2 = { id: 1, name: 'John', address: { city: 'New York', state: 'CA' } }

    * match response1 contains deep response2

# Note: My requirement is to show no difference at all as the state node should be ignored in the karate report.

I am not sure what exactly should i try here for ignoring the field. Please help here.

1

There are 1 best solutions below

2
Peter Thomas On

tp show difference of city only in the karate report

Sorry, Karate doesn't work like that. Teams use it for schema validation like this, and by the way, this is the answer to your "Scenario 2".

* match response contains deep { address: { city: '#string' } }

What you are asking for can be done by code. Or you are welcome to contribute code to Karate.

Here is how you can re-write Scenario 2:

  * def response1 = { id: 1, name: 'John', address: { city: 'New York', state: 'NY' } }
  * def response2 = { id: 1, name: 'John', address: { city: 'New York', state: 'CA' } }
  * response2.address.state = '#string'
  * match response1 == response2