尝试在 API 响应中提取满足某些过滤条件的元素时,空手道 json 路径过滤器不起作用

Karate json path filter not working when trying to extract elements that meet some filter criteria in API response

我正在尝试使用 JSON 路径过滤器过滤我的 API 响应,使用空手道框架来提取满足使用变量值的条件之一的特定值,但我无法正确映射变量,所以我的过滤器不能正常工作。我查看了文档并尝试了多种方法,但无法解决此问题。

响应示例 JSON:

  "slices": [
    {
      "id": 7591164138534052,
      "duration": {
        "value": 1675,
        "unit": "MINUTE"
      },
      "segments": [
        {
          "id": 1,
          "segmentRefId": 23783268357325705
        },
        {
          "id": 2,
          "segmentRefId": 7591164138531002
        },
        {
          "id": 3,
          "segmentRefId": 7591164138532394
        }
      ]
    }
  ],
  "segments": [
    {
      "id": 23783268357325705,
      "departDateTime": "2019-10-05T19:50:00",
      "arrivalDateTime": "2019-10-06T14:25:00",
      "originAirport": "LAX",
      "destinationAirport": "LHR",
      "duration": {
        "value": 635,
        "unit": "MINUTE"
      },
      "marketingAirline": "BA",
      "operatingAirline": "AA",
      "flightNumber": "1509",
      "equipmentCode": "77W",
      "subjectToGovtApproval": false,
      "numOfStops": 0,
      "stops": []
    }
  ]
}```

I am using the below script where I am using variable 'originRefId':

* def originRefId = response.slices[0].segments[0].segmentRefId
* def origin = karate.jsonPath(response, "$.segments[?(@.id=='originRefId')]")
* print 'the value of Origin is:', origin


Expected results  LAX but I am getting an empty array.
17:42:15.119 [main] INFO  com.intuit.karate - [print] the value of Origin is: [
]

这是您的解决方案:

* def originRefId = response.slices[0].segments[0].segmentRefId
* def segment = karate.jsonPath(response, "$.segments[?(@.id==" + originRefId + ")]")[0]
* def origin = segment.originAirport
* match origin == 'LAX'