In this tutorial we will talk about the advance usage of JSONPath in Rest Assured which is How to DeSerialize JSON Array to List. If you have reached here directly, I would suggest that you go through these links before starting with this.
DeSerialize JSON Array to List
Earlier we saw how JSONPath can be used to make a deterministic and accurate search in the Response JSON. With this capability, we are able to write better test validations.
In this chapter, we will focus on how we can perform an advance search in the Response JSON by Deserialize JSON Array to List. We will also learn how we can represent the searched section of Response in various Java data structures. This tutorial is structured based on different methods available in JSONPath class.
DeSerialize JSON Array to List of String using JSONPath
JsonPath class has two overloaded methods (jsonPath.getList) to extract JSON nodes as a Java List. Here are the methods
- JsonPath.getList(String) method lets us get the searched nodes as a List of String
- JsonPath.getList(String, Class T) method let us get the searched nodes as a List of T type
To understand we will make a Get call to the http://restapi.demoqa.com/utilities/books/getallbooks REST service. This service returns a JSON response consisting of a collection of books(Array of Books). Each book in the collection is a JSON object containing values describing the book. You can directly open the URL in the browser to see the output.
To understand JsonPath.getList(String) method we will try to retrieve all the books as a List of String (List<String>
). All we need to do is give "books" as the JSONPath.
Below is the code
@Test
public void JsonPathUsage() throws MalformedURLException
{
RestAssured.baseURI = "https://restapi.demoqa.com/utilities/books/getallbooks";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// First get the JsonPath object instance from the Response interface
JsonPath jsonPathEvaluator = response.jsonPath();
// Read all the books as a List of String. Each item in the list
// represent a book node in the REST service Response
List<String> allBooks = jsonPathEvaluator.getList("books.title");
// Iterate over the list and print individual book item
for(String book : allBooks)
{
System.out.println("Book: " + book);
}
}
Once you run this, the output will be look like this:
Book: Eloquent JavaScript, Second Edition
Book: Learning JavaScript Design Patterns
Book: Speaking JavaScript
Book: Programming JavaScript Applications
Book: Understanding ECMAScript 6
Book: You Don't Know JS
Book: Git Pocket Guide
Book: Designing Evolvable Web APIs with ASP.NET
DeSerialize JSON Array to List of Class (Type T) Object using JSONPath
We all are familiar with Serialization and De-serialization support built-in in Rest-Assured. Let us try to convert the searched nodes directly into an object representation. In this example let us retrieve all the books from the JSON response. We will retrieve all the books as a List of Books class. In order to do this, we will first create a Class Representation of a Book. Get all the properties of JSON Book entity and create a class with those member variables. A book can be simply represented by a POJO class as shown in the code below.
public class Book {
String isbn;
String title;
String subtitle;
String author;
String published;
String publisher;
int pages;
String description;
String website;
}
Now to get the response converted into a List of Books we will simply use the JsonPath.getList(String, Class T) method.
- First parameter is the JSONPath ("books") in this case
- Second parameter is the Type name to which we want the response to be converted. Book class in this case.
Below is the complete code showing the usage
@Test
public void JsonPathUsage() throws MalformedURLException
{
RestAssured.baseURI = "https://restapi.demoqa.com/utilities/books/getallbooks";
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("");
// First get the JsonPath object instance from the Response interface
JsonPath jsonPathEvaluator = response.jsonPath();
// Read all the books as a List of String. Each item in the list
// represent a book node in the REST service Response
List<Book> allBooks = jsonPathEvaluator.getList("books", Book.class);
// Iterate over the list and print individual book item
// Note that every book entry in the list will be complete Json object of book
for(Book book : allBooks)
{
System.out.println("Book: " + book.title);
}
}
This is a very good feature of Rest-Assured that enables us to get a targeted part of the response.