RestAssured - Get values from nested object using Groovy gson path

577 Views Asked by At

How to use rest assured to get the list of all the zips from the below below structure?

{
    "persons":[
        {
            "name": "",
            "age":"",
            "addresses": [
                {
                    "city": "..",
                    "zip": ".."
                }
            ]
        }
    ]
}

Expected result is List<String> which contains zip codes only using groovy gson path.

2

There are 2 best solutions below

4
On BEST ANSWER

This would work:

import io.restassured.path.json.JsonPath;
...

List<List<String>> temp = JsonPath.from(res).getList("persons.addresses.zip");
List<String> zips = temp.stream().flatMap(List::stream).collect(Collectors.toList());
System.out.println(zips);
//[..]
2
On

You could use flatten() method to get the zip code:

List<String> zips =JsonPath.from(res).getList("persons.addresses.flatten().zip")