Java – Karate DSL asserts nested json

Karate DSL asserts nested json… here is a solution to the problem.

Karate DSL asserts nested json

{"serviceName":"Legal Entity account for given input account.","requestTime":1545426348945,"responseTime":1545426348949,"timeTaken":4,"responseCode":0," responseMessage":"Success","pageSize":100,"pageNumber":0,"accounts":{"transferDate":1549429200000,"migrationWave":"5","searchedLEAccount":{"accountNumber":"41477514", "cbdNumber":"12345678","bic":"CHASGBXxX","poolAccount":"Y","sweepMasterAccount":"Y","status":"DORMANT","branchId":"000000071","branchName":"LONDON","leAccountType":" OLD"},"linkedLEAccount":{"accountNumber":"6541245045","cbdNumber":"854321","bic":"CHASLUY","status":"DORMANT","branchId":"000000055","branchName":"S.A","leAccountType ":"NEW"}}}

I’m trying to get all accountNumber and verify if they’re numbers. What am I doing wrong?

When method Post

Then status 200

And match response != null

And match response contains {serviceName: 'Legal Entity account for given input account.' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#notnull' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#present' }

And match response.accounts.searchedLEAccount contains { accountNumber: '#number' }

Solution

One line:

* match each $.. accountNumber == '#regex \\d+'

Tip: Read the documentation carefully and understand JSON-Path.

This is a complete example that you can paste into a new scenario and see how it works:

* def response = 
"""
{
   "serviceName":"Legal Entity account for given input account.",
   "requestTime":1545426348945,
   "responseTime":1545426348949,
   "timeTaken":4,
   "responseCode":0,
   "responseMessage":"Success",
   "pageSize":100,
   "pageNumber":0,
   "accounts":{
      "transferDate":1549429200000,
      "migrationWave":"5",
      "searchedLEAccount":{
         "accountNumber":"41477514",
         "cbdNumber":"12345678",
         "bic":"CHASGBXxX",
         "poolAccount":"Y",
         "sweepMasterAccount":"Y",
         "status":"DORMANT",
         "branchId":"000000071",
         "branchName":"LONDON",
         "leAccountType":"OLD"
      },
      "linkedLEAccount":{
         "accountNumber":"6541245045",
         "cbdNumber":"854321",
         "bic":"CHASLUY",
         "status":"DORMANT",
         "branchId":"000000055",
         "branchName":"S.A",
         "leAccountType":"NEW"
      }
   }
}
"""
* match each $.. accountNumber == '#regex \\d+'

Related Problems and Solutions