In this tutorial we will talk about the advance usage of JSONPathin Rest Assured which is How toDeSerialize 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 JSONPathcan 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 Array to List. We will also learn how we can represent the searched section of Responsein 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

JsonPathclass 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/getallbooksREST 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 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.

We all are familiar with Serializationand 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 POJOclass as shown in the code below.

1

2

3

4

5

6

7

8

9

10

11

12

publicclassBook{

Stringisbn;

Stringtitle;

Stringsubtitle;

Stringauthor;

Stringpublished;

Stringpublisher;

intpages;

Stringdescription;

Stringwebsite;

}

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.

Author: Virender Singh

I am Virender Singh and I am a software Engineer.I have been in the Software profession for more than 12 years now. I worked on large spectrum of projects, from being a QA engineer to being a Development Engineer. At present I work as a Software Engineer for Microsoft India Development centre. I love to learn new technologies, specially in the field of Image Processing and Digital Signal processing. You can find me at following locations @LinkedIn@FaceBook@ToolsQA@Github

Have passed 11 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently I am working with BLOOMREACH as SDET.

I am passionate about designing Automation Frameworks that are effective and easy to maintain. For automating websites my weapons are QTP and Selenium (Webdriver). I live in Amsterdam(NL), with my wife and a lovely daughter.