空手道中的未命名 JSON 数组字段验证

Un-named JSON array field validation in Karate

我有一个来自响应的未命名 JSON 数组,想检查它是否包含 "confirmationNumber": "pqrs"。我可以知道如何在空手道中检查吗?

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

karate.filter() 适用于这些情况:

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

另见 JsonPath 示例:https://github.com/intuit/karate#jsonpath-filters

编辑:抱歉,有一个更简单的方法,请 read the docs !

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