Sadiq K
Sadiq K

Reputation: 192

Karate DSL assert on 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 am trying to grab all accountNumber and validate if they are 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' }

Upvotes: 3

Views: 923

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

In one line:

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

Tip: read the docs carefully and understand Json-Path.

Here's the full example which you can paste into a new Scenario and see working:

* 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+'

Upvotes: 3

Related Questions