11/16/17

Deserialize JSON Response as List

In this tutorial we will talk about the advance usage of JSONPath in Rest Assured. If you have reached here directly, I would suggest that you go through these links before starting with this.
  • REST API tutorial set
  • Basic JsonPath tutorial
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 advance search in the Response JSON by Deserialize JSON Response as 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.

 

Search Json Response nodes as a List using JSONPath

JsonPath class has two overloaded methods (jsonPath.getList) to extract JSON nodes as a Java List. Here are the methods
Deserialize JSON Response as List
  • 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. Each book in the collection is a JSON object containing values describing the book. You can directly open the URL in 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). All we need to do is give “books” as the JSONPath.
Below is the code
@Test
public void JsonPathUsage() throws MalformedURLException
{
    RestAssured.baseURI = "http://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 allBooks = jsonPathEvaluator.getList("books.title");

    // Iterate over the list and print individual book item
    for(String book : allBooks)
    {
        System.out.println("Book: " + book);
    }
}

One 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 

Search JSON Response nodes as list of Type T 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 represent 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. Bookclass in this case.
Below is the complete  code showing the usage
@Test
public void JsonPathUsage() throws MalformedURLException
{
    RestAssured.baseURI = "http://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 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 get targeted part of the response.

0 comments:

Post a Comment

Google Q&A Forum