ernitingoel
ernitingoel

Reputation: 661

Un-named JSON array field validation in Karate

I have a un-named JSON array like this from the response and would like to check whether it contains "confirmationNumber": "pqrs" or not. May I know how can I check that in Karate?

[
    {
        "id": 145,
        "confirmationNumber": "abcd"
    },{
        "id": 723
        "confirmationNumber": "pqrs"
    }
    ,{
        "id": 7342
        "confirmationNumber": "sfeq"
    }
]

Upvotes: 1

Views: 187

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

karate.filter() is good for these situations:

* def response = 
"""
[
   {
      "id":145,
      "confirmationNumber":"abcd"
   },
   {
      "id":723,
      "confirmationNumber":"pqrs"
   },
   {
      "id":7342,
      "confirmationNumber":"sfeq"
   }
]
"""
* def fun = function(x){ return x.confirmationNumber == 'pqrs' }
* def found = karate.filter(response, fun)
* match found == '#[1]'

Also see examples of JsonPath: https://github.com/intuit/karate#jsonpath-filters

EDIT: apologies, there is a much simpler way, please read the docs !

* match response contains { id: '#number', confirmationNumber: 'pqrs' }
* def item = { confirmationNumber: 'pqrs' }
* match response contains '#(^item)'

Upvotes: 2

Related Questions