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
- Send a GET to
/usersand keep theResponse. - Call
getList()withnameand store the result in aList. - Do the same with
city. - Print the names after
Names:and the cities afterCities:, 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 aList<String>compiles, then throwsClassCastExceptionwhen an element is read as a string. UsegetList()withInteger.class, or withString.classif 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.
Related lessons
- hasItem and hasItemsAssert membership in the same collected lists with
hasItem()andhasItems(). - Sorting ResultsCollect names from a search sorted in descending order.
- Products by CategoryCollect product names from a category endpoint.
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 /users
// TODO: Extract all names using getList("name")
// TODO: Extract all cities using getList("city")
// TODO: Print both lists
}
}