Reputation: 11
I need to extract an element based on a condition inside a nested JSON response.
{
"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.
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":
$.travelers[?(@.seatAvailabilityStatus=="available")].id
But in my case, if the seatAvailabilityStatus is available I need to get the "seatNumber". Can anyone help?
Screen shot of the JSON I included
Upvotes: 0
Views: 283
Reputation: 167992
You need to get 1 level up because JsonPath doesn't know anything about parents of the matched attribute
Suggested query:
$..[?(@.travelers[?(@.seatAvailabilityStatus =='available')])].seatNumber
Demo:
More information: How to Use the JSON Extractor For Testing
Upvotes: 0
Reputation: 42
You could try this way:
$..[?(@.travelers[?(@.seatAvailabilityStatus =='available')] empty false)].seatNumber
Upvotes: 1