sam
sam

Reputation: 289

Karate API : How to assert a json/response node value which returns null or an array object for different data

I've got a request that returns a response node(itemCanBe) value in two possible ways, depending on the 'itemNum'. How do I assert this ? below attempt is not working

* match res..itemCanBe == null || res..itemCanBe[*] contains ['Colgate']

itemCanBe returning null

{
"itemDetails": {
    "1234": {
        "itemNum": "1234",
        "itemCanBe": null
    }
  }
}

itemCanBe returning array

{
"itemDetails": {
    "4567": {
        "itemNum": "4567",
        "itemCanBe": [
            "Colgate",
            "Sensodine"
        ]
    }
}

}

Upvotes: 1

Views: 924

Answers (2)

Soumya
Soumya

Reputation: 21

See this thread for details: https://github.com/intuit/karate/issues/1202#issuecomment-653632397 This should be good enough to solve your problem.

Upvotes: 2

Peter Thomas
Peter Thomas

Reputation: 58058

This will actually work:

* def temp = get[0] response..itemCanBe
* match temp == '##[]'

Also refer: https://stackoverflow.com/a/50350442/143475

Upvotes: 1

Related Questions