0

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

2 Answers 2

1

You could try this way:

$..[?(@.travelers[?(@.seatAvailabilityStatus =='available')] empty false)].seatNumber

Sign up to request clarification or add additional context in comments.

Comments

0

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:

enter image description here

More information: How to Use the JSON Extractor For Testing

1 Comment

with this query even if the seatAvailabilityStatus in unavailable it will still output the same result. as mentioned by @Venkatesan you also need to compare the inner most filtered object with empty false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.