javarest-assuredrest-assured-jsonpathjavax.json

How to write a GPath expression when the RestAssured response is simply an array with out a name


After my RestAssured code executed, I got a response body like this(which is valid, since I verified it in Postman also)

[
    {
        "team": null,
        "name": "Automation",
        "id": "977638c2-0095-42fb-a3fb-3ef442cc61e2"
    },
    {
        "team": null,
        "name": "Default",
        "id": "e9e0ab5d-0abf-402a-a747-612a4e9c7e25"
    }
]

Now I need to retrieve the id of a Json object for which the name is 'Automation'. The line of code I wrote is like this which is returning null, which I can't understand.

response.path("$.find{it.name == 'Automation'}.id");  (OR)
response.path("content.find{it.name == 'Automation'}.id");

Please correct my code, to return the correct value 977638c2-0095-42fb-a3fb-3ef442cc61e2


Solution

  • You can use the query like this:

    response.path("find{it.name == 'Automation'}.id");