Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAssured JsonPath: flat array of elements in hierarchy

I'm writing a smoke test using rest-assured and want to traverse the api to make sure no unexpected errors occur.

I have a data structure that looks like this:

{
...
"sites": [
    {
        ...
        "groups": [
            {
                ...
                "locations": [
                    {
                        ...
                        "racks": [
                            {
                                "rackId": 123456789,
                                ...
                            },
                            {
                                "rackId": 987654321,
                                ...
                            },
                            ...
                        ]
                    }
                ]
            }
        ]
    },
    {
        ...
        "groups": [
            {
                ...
                "locations": [
                    {
                        ...
                        "racks": [
                            {
                                "rackId": 11111111,
                                ...
                            },
                            {
                                "rackId": 22222222,
                                ...
                            },
                            ...
                        ]
                    }
                ]
            }
        ]
    },
    ...
]
}

Using JsonPath bundled in RestAssured I'm trying to get a flat list of all rackIds to then call subsequent requests for these rackIds.

jsonPath.getList("sites.groups.locations.racks.rackId", Long.class);

>> java.lang.NumberFormatException: For input string: "[[[406071537, 406071538, 406071539, 406071540, 406071541]]]"

I tried using this path, but didn't work because I believe this syntax only works with the other JsonPath implementation, not the one bundled with rest-assured

"$.sites[*].groups[*].locations[*].racks[*].rackId"

I now came down to this, that gives me lists of lists of lists, that I could then flatten myself. But I then have the issue, that the numbers interpreted as Integers by default, but I'm receiving Long values.

 List list = jsonPath.getList("sites.groups.locations.racks.rackId");

Any ideas?

like image 741
Ranil Wijeyratne Avatar asked Jan 06 '23 10:01

Ranil Wijeyratne


1 Answers

Just use flatten():

List list = jsonPath.getList("sites.groups.locations.racks.rackId.flatten()");
like image 61
Johan Avatar answered Jan 19 '23 04:01

Johan