Skip to content
CodeKruPro

Extract List of Values

15/62

Extract List of Values

When an endpoint returns an array of objects, you can extract a specific field from every object:

List<String> titles = response.jsonPath().getList("title");
// Returns the "title" value of every object in the array, in order

Task: GET /users and extract all names and all cities as lists.

Expected output:

Names: [John Doe, Jane Smith, Bob Wilson, Alice Brown, Charlie Davis]
Cities: [New York, London, Paris, Berlin, Tokyo]

About this lesson

REST Assured: extract all values of a JSON field across an array of objects

Learn REST AssuredIntermediateJSON Path Extraction

How it works

When the response is an array of objects, a bare field name is a GPath expression that visits every element. getList("name") on the user list therefore returns all five names, in the order the server sent them. Element types follow the JSON: names come back as String, while getList("id") holds Integer values.

GPath also filters. A closure such as findAll { it.age > 30 }.name keeps only matching objects and then collects their names, and find returns the first match only. A field that is absent everywhere gives a list of nulls, one per element, rather than null or an empty list.

Java generics are erased at run time, so getList() cannot check your declared element type. A List<String> filled from age holds Integer values and fails only when you read an element as a String.

About the assignment

From the full user list you build two Java lists in one pass: every name and every city. Printing each list after its label shows the values in server order.

What you will do

  1. Send a GET to /users and keep the Response.
  2. Call getList() with name and store the result in a List.
  3. Do the same with city.
  4. Print the names after Names: and the cities after Cities:, relying on the list's own bracketed format.

Common mistakes

  • Looping and building the list by hand

    Requesting each user and adding names one at a time sends five requests where one would do. A single getList() call on the collection returns them all.

  • Declaring the wrong element type

    Storing getList("age") in a List<String> compiles, then throws ClassCastException when an element is read as a string. Use getList() with Integer.class, or with String.class if you want converted text.

  • Treating a list of nulls as empty

    A misspelt field returns five nulls, so isEmpty() is false and the size still looks right. Check a real value, not only the count.

In a real test suite

Collected lists are handy for comparisons a matcher cannot express easily, such as checking that names are sorted after a sort parameter. They are also the input for data-driven follow-ups, where each extracted id becomes a request of its own.

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /users
        // TODO: Extract all names using getList("name")
        // TODO: Extract all cities using getList("city")
        // TODO: Print both lists
    }
}