本文介绍了尝试提取符合API响应中某些筛选条件的元素时,空手道json路径筛选器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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'

这篇关于尝试提取符合API响应中某些筛选条件的元素时,空手道json路径筛选器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:08