How to transform the response to the Java list in Rest Assured?


We can transform the response to Java list in Rest Assured. This can be achieved when we have a JSON array Response. To convert the JSON array to List, we need to use the method as.(List.class).

Once the JSON array Response is converted to a List, we need to convert it to a Map and get all values in the Response in a key-value pair. We shall first send a GET request via Postman on a mock API URL and go through the JSON Response array.

Example

Code Implementation

import java.util.List;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
public class NewTest {
   @Test
   public void convertResponsetoList() {

      //base URL
      RestAssured.baseURI = "https://run.mocky.io/v3";

      //convert JSON Response array to List
      List<Object> l = RestAssured

      //GET request on Mock URL
      .get("/1bb42856-4583-4c18-91ed-b9a6ab19efb4")
      .as(List.class);

      //size of List
      int s = l.size();
      System.out.println("List size is: " + s);
   }
}

Output

Updated on: 22-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements