0

I am using the com.jayway.restassured.response.Response class to read the response from the API. Its in the following format. Is there a way I can access the value of layout or regions using the keyname. For example - response.layout or response.regions? I tried using the JSONSlurper but that didnt work with the Response class. Any help is greatly appreciated.

JSON Response :
{
layouts:
    [
        regions:
            [
                [
                    metadata:null, 
                    endDate:null, 
                    displayName:null, 
                    roles:[], type:100, 
                    widgets:[], 
                    structure:100, 
                    repositoryId:headerRegionHomePage, 
                    name:header,
                    width:12, 
                    audiences:[], 
                    startDate:null, 
                    height:300
                ],
                [
                    structure:100, 
                    type:101, 
                    widgets:[], 
                    width:12
                ], 
                [
                    metadata:null, 
                    endDate:null, 
                    displayName:null, 
                    roles:[], 
                    type:102, 
                    widgets:[], 
                    structure:100, 
                    repositoryId:footerRegionHomePage, 
                    name:footer, 
                    width:12, 
                    audiences:[], 
                    startDate:null, 
                    height:300
                ]
            ],
        ]
    }
Scorned Rider
  • 11
  • 1
  • 5

3 Answers3

0

Try converting directly the API response into json array :
JSONArray JSONResponseBody = new JSONArray(response.body().asString());
Then you should be able to access the keys to retrieve the desired values.

Matt
  • 21
  • 4
0
    String RequiredvaluefromJSON = new JsonPath(RestAssured.
                    given().
                    get("URI").
                    andReturn().
                    asString()).get("Layout.regions");
            System.out.println(RequiredvaluefromJSON);

Given your API in URI, this will fetch you the regions[] from JSON. Let me know if this is of any use.
k.o
  • 1
0

An elegant way could be using de-serialization.

Create a POJO for the response json. You can use Gson/Jackson to de-serialize your response to a class type or Rest Assured also gives inbuilt mechanism for de-serialization.

Response response = request.post("/");
ResponseBody body = response.getBody();

    // Deserialize the Response body into ClassType
ClassName responseBody = body.as(ClassName.class);

Have getters() to extract the corresponding field name from the response.