About this lesson
REST Assured: deserialize a JSON array into a Java array of objects
Learn REST AssuredAdvancedSerialization and Deserialization
How it works
Collection endpoints usually return a top-level JSON array rather than an object. To map one, ask for an array type: as() with User[].class turns each element into one User and keeps the order of the JSON. The result is a normal Java array, so length gives the count and an enhanced for loop visits every element.
The target type has to match the top-level shape. Asking for a single User when the body starts with [ fails with a Jackson MismatchedInputException saying it cannot deserialize the class out of a START_ARRAY token, wrapped in a RuntimeException by REST Assured. The opposite mismatch, an array type for a single object, fails as well.
The same strictness about unknown keys applies to every element. Because testkru /users returns the five fields the starter class declares for each user, the mapping succeeds. If one element carried an extra key, the whole call would throw, not just that element.
About the assignment
Turn the full list of users into a Java array, report how many there are, and print each user's name with their city.
What you will do
- GET
/usersand callas()with the array classUser[].class, directly or afterextract(). - Print the array's
lengthafterTotal users:. - Loop over the array and, for each user, print the name, a space, a hyphen, a space and the city on one line.
Common mistakes
Asking for User.class
A single class for an array body fails with a mismatched input error. Read the first character of the response:
[means you need an array or a list type.Calling size() on an array
Java arrays have a
lengthfield, not asize()method. Mixing the two causes a compile error, and it is easy to do after working withgetList()results.Hard-coding five lines
Printing five fixed users instead of looping hides a changed dataset. Drive the output from the array so that the count line and the user lines always agree with what the API sent.
In a real test suite
Once a collection is in a typed array or list, assertions become ordinary Java: stream it to check that every user has an email, find one by id, or compare it with an expected list. If you prefer a List, wrap the array with Arrays.asList().
Related lessons
- Deserialize Response to POJODeserializes a single user object into the same class.
- Extract List of ValuesReads one field from every array element with JsonPath instead.
- Validate Array SizeChecks the size of the same users array with a GPath size() check instead of mapping it.
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.*;
public class Main {
static class User {
public int id;
public String name;
public String email;
public int age;
public String city;
}
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /users and deserialize to User[] array
// TODO: Print total count
// TODO: Loop through and print "name - city" for each user
}
}