Search code examples
rest-assuredrest-assured-jsonpath

RestAssured - Get values from nested object using Groovy gson path


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.


Solution

  • 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);
    //[..]