How to get the size of an array within a nested JSON in Rest Assured?

We can get the size of an array within a nested JSON in Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.

Finally, to obtain JSON array size, we have to use the size method. We shall send a GET request via Postman on a mock API, and observe the Response.

Using Rest Assured, let us get the size of the Location array within the nested JSON response. The size should be three since it contains information about three locations - Michigan, Indiana, and New York.


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 {
   public void jsonArySize() {

      //base URI with Rest Assured class
      RestAssured.baseURI = "";

      //obtain Response from GET request
      Response res = given()

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

      //length of JSON Location array
      int s = j.getInt("Location.size()");
      System.out.println("Array size of Location: " +s);


Updated on: 17-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started