Skip to content
CodeKruPro

Extract from Books with Tags

14/62

Extract from Books with Tags

The /books/1 endpoint returns a book with a nested author object and a tags array:

{"id": 1, "title": "The Great Gatsby",
 "author": {"name": "F. Scott Fitzgerald", "nationality": "American"},
 "tags": ["fiction", "classic", "literature"], "rating": 4.5}

Use getList() with the array's path to extract it as a Java List, and dot notation for the author's name.

Task: Extract and print the title, author name, and tags.

Expected output:

Title: The Great Gatsby
Author: F. Scott Fitzgerald
Tags: [fiction, classic, literature]

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

  1. Send a GET to /books/1 and keep the Response.
  2. Read title and the nested author.name with getString().
  3. Read tags with getList() into a List of strings, java.util.List is already imported.
  4. Print the title, the author and the list after Title:, Author: and Tags:.

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 a List.

  • Reading the author object as text

    getString("author") returns a Groovy map string in square brackets, not the author's name. Add the name segment to the path.

  • Assuming a missing array is empty

    getList() on a key that does not exist returns null, not an empty list, so a loop over it throws NullPointerException.

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.

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 /books/1
        // TODO: Extract title, author.name (using getString), and tags (using getList)
        // TODO: Print each
    }
}