Murugesh Subramaniam
Murugesh Subramaniam

Reputation: 313

How to do comparison of json object on nesting level in karate

how to compare a nested json response with another response

actualResponse:

  {
    "status": "SUCCESS",
    "accountOrder": {
               "id": "11450027922158000",
               "accountType": "SAVINGS",
               "servicePrice": 400
               }
     }

expectedReponse:

 {
    "status": "SUCCESS",
    "accountOrder": {
               "accountType": "SAVINGS"
     }
     }

I have tried the following methods for comparison:

   * match actualResponse contains expectedResponse // failed with reason: actual value has 2 more key(s) than expected

Our requirement is we don't want to hardcode comparison like

 * match actualResponse.accountType contains expectedResponse.accountType

We may need to add more attributes in expected and don't want to modify the script frequently

Upvotes: 1

Views: 498

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Please read the docs: https://github.com/intuit/karate#contains-short-cuts

* def accountOrder = { "accountType": "SAVINGS" }
* def expected = { status: SUCCESS, accountOrder: '#(^accountOrder)' }
* def response =
"""
{
  "status": "SUCCESS",
  "accountOrder": {
    "id": "11450027922158000",
    "accountType": "SAVINGS",
    "servicePrice": 400
  }
}
"""
* match response == expected

Upvotes: 2

Related Questions