[英]How to extract nested elements in JSON response using JSON extractor
I need to extract an element based on a condition inside a nested JSON response.我需要根据嵌套 JSON 响应中的条件提取元素。
{
"cabin":"eco",
"seatNumber":"15A",
"travelers":[
{
"id":"7789",
"seatCharacteristicsCodes":[
"CH",
"W"
],
"seatAvailabilityStatus":"available",
"prices":[
{
"base":40,
"total":40,
"currencyCode":"AED",
"totalTaxes":0
}
]
}
],
"coordinates":{
"x":1,
"y":0
}
}
I need to check the "seatAvailabilityStatus" as "available" or not (which is inside "travelers"), if it is available, I need to get the "seatNumber" and proceed with the flow.我需要检查“seatAvailabilityStatus”是否为“可用”(位于“旅行者”内部),如果可用,我需要获取“seatNumber”并继续流程。
I have seen some example and tried like the below, but I am able to capture only the values which are associated to "travelers" like "id" or "prices":我看过一些示例并尝试如下,但我只能捕获与“旅行者”相关的值,如“id”或“prices”:
$.travelers[?(@.seatAvailabilityStatus=="available")].id
But in my case, if the seatAvailabilityStatus is available I need to get the "seatNumber".但就我而言,如果座位可用状态可用,我需要获取“座位号”。 Can anyone help?
任何人都可以帮忙吗?
You could try this way:你可以这样尝试:
$..[?(@.travelers[?(@.seatAvailabilityStatus =='available')] empty false)].seatNumber
You need to get 1 level up because JsonPath doesn't know anything about parents of the matched attribute您需要升级 1 级,因为 JsonPath对匹配属性的父级一无所知
Suggested query:建议查询:
$..[?(@.travelers[?(@.seatAvailabilityStatus =='available')])].seatNumber
Demo:演示:
More information: How to Use the JSON Extractor For Testing更多信息:如何使用 JSON 提取器进行测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.