About this lesson
REST Assured: reading a JSON array into a Java List with getList()
Learn REST AssuredIntermediateJSON Path Extraction
How it works
A JSON array inside an object becomes a Java collection with getList(). The method returns a List you can loop over, size, or print, and its toString() lists the elements in square brackets separated by commas. getList() with a class argument, such as String.class, also converts each element to that type.
Array paths support indexes. tags[0] reads the first element and tags[-1] reads the last, while tags.size() returns the count. Nested objects mix freely with arrays, so a single response can be read with author.name, tags, and rating side by side. getString() on the whole array also works, but gives you one String, not a list you can loop over.
About the assignment
Book 1 combines a plain title, an author object and an array of tags. You request it, read the title, the author's name and the tags as a list, and print all three with their labels.
What you will do
- Send a GET to
/books/1and keep theResponse. - Read
titleand the nestedauthor.namewithgetString(). - Read
tagswithgetList()into aListof strings,java.util.Listis already imported. - Print the title, the author and the list after
Title:,Author:andTags:.
Common mistakes
Using getString() for the tags
It produces text that looks like a list when printed, which hides the mistake. As soon as you need to loop over the tags or check their size, you have a
String, not aList.Reading the author object as text
getString("author")returns a Groovy map string in square brackets, not the author's name. Add thenamesegment to the path.Assuming a missing array is empty
getList()on a key that does not exist returnsnull, not an empty list, so a loop over it throwsNullPointerException.
In a real test suite
Lists extracted this way often feed Hamcrest checks such as hasItem() or hasSize(), or are compared with expected values from test data. When tags or similar arrays drive behaviour, such as filters in a search API, a typed POJO field of List type is the long-term home for them.
Related lessons
- Extract Nested ValuesRead single values from nested objects with the typed getters.
- Extract List of ValuesCollect one field from every element of a top-level array.
- Filter by CategoryFilter the search endpoint to books and list their names.
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.
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 /books/1
// TODO: Extract title, author.name (using getString), and tags (using getList)
// TODO: Print each
}
}