gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:37:07 25 4
gpt4 key购买 nike

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

示例响应 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 - 尝试提取满足 API 响应中某些过滤条件的元素时, Karate json 路径过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com