Extracting anonymous array problem

730 views
Skip to first unread message

Mikael Wallin

unread,
Apr 15, 2016, 5:17:47 AM4/15/16
to REST assured

Anyone who can help me solve this. 


I get:

java.lang.IllegalArgumentException: Invalid JSON expression:

Script1.groovy: 1: unexpected token: ] @ line 1, column 30.

                            $[*]

                                ^


My code:

List<Browser> browsers

    given()

        .log().ifValidationFails()

        .accept("application/json; charset=utf-8")

    .when()

        .get("/jobs")

    .then()

        .statusCode(HttpStatus.SC_OK)

        .body("size()", greaterThan(0))

    .extract().body().jsonPath().getList("$[*]", Browser.class);


My json: (from http://localhost/browsers)

[

{

"browser_version":"16.0",

"browser":"firefox",

"os_version":"8",

"os":"Windows",

"device":null

},

{

"browser_version":"17.0",

"browser":"firefox",

"os_version":"8",

"os":"Windows",

"device":null

},

{

"browser_version":"18.0",

"browser":"firefox",

"os_version":"8",

"os":"Windows",

"device":null

}

]

Johan Haleby

unread,
Apr 15, 2016, 5:21:42 AM4/15/16
to rest-a...@googlegroups.com
You're trying to use syntax from the json-path project but this is not the syntax (confusingly) that REST Assured is using. REST Assured uses GPath expressions as indicated by the documentation. So your example would probably read something like:

body().jsonPath().getList("$", Browser.class);

but you can ignore $ as well I think:

body().jsonPath().getList("", Browser.class);

Regards,
/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikael Wallin

unread,
Apr 15, 2016, 6:06:17 AM4/15/16
to REST assured
Thank you for your reply. It was very helpful.

If I use $ I get java.lang.ClassCastException: Cannot convert class java.util.HashMap to class se.svt.test.screenshots.model.Browser.

Johan Haleby

unread,
Apr 15, 2016, 6:13:38 AM4/15/16
to rest-a...@googlegroups.com
Does it work if you exclude $?

On Fri, Apr 15, 2016 at 12:06 PM, Mikael Wallin <mikael....@gmail.com> wrote:
Thank you for your reply. It was very helpful.

If I use $ I get java.lang.ClassCastException: Cannot convert class java.util.HashMap to class se.svt.test.screenshots.model.Browser.

--

Mikael Wallin

unread,
Apr 15, 2016, 6:25:02 AM4/15/16
to REST assured
Same result.

Does GPath refer to Goessner? I was trying to find resources to read up on.



Mikael Wallin

unread,
Apr 15, 2016, 6:40:14 AM4/15/16
to REST assured
I solved the issue. Thank you very much for your help. It seems like the problem was that the test run before the context of the server was loaded. So your solution was correct.

Johan Haleby

unread,
Apr 15, 2016, 7:05:49 AM4/15/16
to rest-a...@googlegroups.com
Are you sure? :) The path should be correct but does it really work when you try to map the list content to a java object? Because I actually tried to something similar extracting which extracted  java objects from a list in with JsonPath and this was previously not supported (afaict). Something like this: JsonPath.from(someJson).getList("store.books", Book.class); I've added support for it now though and so if you like you can try out version 2.9.1-SNAPSHOT after having added the following repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <snapshots />
        </repository>
</repositories>


On Fri, Apr 15, 2016 at 12:40 PM, Mikael Wallin <mikael....@gmail.com> wrote:
I solved the issue. Thank you very much for your help. It seems like the problem was that the test run before the context of the server was loaded. So your solution was correct.

--

Mikael Wallin

unread,
Apr 15, 2016, 7:16:42 AM4/15/16
to REST assured
No not sure :) I just saw that I got an empty array at startup and assumed that was the error as I was somewhat embarrassed :). 

I will check it out. As I am just evaluating rest assured it wasn't too important to solve, but important to know it is possible.

Johan Haleby

unread,
Apr 15, 2016, 7:35:54 AM4/15/16
to rest-a...@googlegroups.com
Alright, tell me if you run into any other problems :) Btw you can read more about gpath here: http://groovy-lang.org/processing-xml.html#_gpath
Reply all
Reply to author
Forward
0 new messages