Explain how to get the size of a JSON array response in Rest Assured.


We can get the size of a JSON array response in Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string. Finally, obtain its length with the size method. Code Implementation

import static io.restassured.RestAssured.given;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class NewTest {
   @Test
   public void jsonAryLen() {

      //obtain Response from GET request
      Response res = given()
      .when()
      .get("https://jsonplaceholder.typicode.com/posts");

      //convert JSON to string
      JsonPath j = new JsonPath(res.asString());

      //length of JSON array
      int s = j.getInt("data.size()");
      System.out.println( s);
   }
}

Output

Updated on: 22-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements