Skip to content
Advertisement

How to find the length or size of the elements of response array in rest assured

I am new to rest assured. I have tried below code for getting response

RestAssured.baseURI = "http://localhost:8090/api";
Response resp = 
          given().
          when().get("/modules")
         .then().extract().response();

This is JSON response

    [
        {
            "moduleid": "1000",
            "module": "reader",
            "title": "Learn to read",
            "description": "Learn to read and understand code"
        },
       {
        "moduleid": "1005",
        "module": "debug",
        "title": "Can you fix it?",
        "description": "Learn how to uncover logical mistakes"
       },
    ]

How do I get the length or size of the elements of that array.

Advertisement

Answer

As above comment, the length here is the number of key-value pair in the json object.

For me, you can choose any object in the array to count the number of key-value pair, but if you want to specify the json object with condition, for example moduleid=1005, this code would work for you.

Map<String, Object> parsed = resp.jsonPath().get("find {it.moduleid = '1005'}");
int numberOfPair = parsed.keySet().size();
System.out.println(numberOfPair); //4
Advertisement