简体   繁体   中英

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.

{
   "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

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

Suggested query:

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

Demo:

在此处输入图像描述

More information: How to Use the JSON Extractor For Testing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM