SaviourSoul SaviourSoul - 1 year ago 86
JSON Question

Rest-assured looking up object from Unnamed Root

I am relatively new to Rest-assured and Gpath. From the JSON below, I just want to get a map with the keys / values for 'RideA'

"state": "open",
"name": "RideA",
"imageCount": 2
"state": "open",
"name": "RideB",
"imageCount": 1
"state": "open",
"name": "RideC",
"imageCount": 2

so far I have tried:

final List<Map<String, ?>> object = from(json).get("it.findAll { == 'RideA' }");

but this returns java.lang.IllegalArgumentException: Cannot get property 'name' on null object , so I think there is something wrong with my syntax

The code below works, but I have to look up the elements with the list by their ordinal number (in this case '0'), when I want to look them up by the 'name' field

final List<Map<String,?>> object = from(json).get("");
assertThat(object.get(0).get("name"), IsEqual.<Object>equalTo("RideA"));

any help is much appreciated!

Answer Source

I finally managed to figure this out, thanks to help from Johan Haleby.

firstly there was a slight error in my first statement, I should have just been looking for a Map, and not a List of Maps.

final Map<String, ?> object = from(json).get("it.findAll { == 'RideA' }");

'findAll' returns a list, so that would not be correct in this case.

The correct statement in this case would be:

final Map<String, ?> object = from(json).get("find { == 'RideA'}");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download