Skip to content
CodeKruPro

Deserialize to Array

46/62

Deserialize to Array

When an endpoint returns a JSON array, deserialize to a Java array:

Book[] books = given().get("/books").then().extract().as(Book[].class);

Each element of the array becomes one object of the class, so you can loop over it with a normal for loop and read the fields.

Task: Deserialize all users and print each name with their city.

Expected output:

Total users: 5
John Doe - New York
Jane Smith - London
Bob Wilson - Paris
Alice Brown - Berlin
Charlie Davis - Tokyo

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

  1. GET /users and call as() with the array class User[].class, directly or after extract().
  2. Print the array's length after Total users:.
  3. 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 length field, not a size() method. Mixing the two causes a compile error, and it is easy to do after working with getList() 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().

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.*;

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
    }
}