top of page

Data Driven Rest Assured Api Testing

For Data Driven REST api testing the below approach has been followed, converting the JSON response from the GET request call to CSV by using the Jackson libraries

Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa

Few resources about Jackson

1) Jackson JSON Processor Wiki- Jackson JSON processor Wiki page

2) Jackson Project Home - Jackson JSON Processor Portal page on GitHub

Data Structure

Before we converting a JSON to CSV, we need to consider how well our data model will map between the two formats. So first, let's consider what data the different formats support:

We use JSON to represent a variety of object structures, including ones that contain arrays and nested objects

We use CSV to represent data from a list of objects, with each object from the list appearing on a new line

This means that if our JSON has an array of objects, we can convert each object into a new line of our CSV file. So, as an example, let's use a JSON containing the following list of Student Details :

[ {

"Student Name" : "Student1",

"Student ID" : 01,

"Grade" : “Frist”

}, {

"Student Name" : "Student2",

"Student ID" : 02,

"Grade" : “Frist”

} ]

We'll use the field names from the JSON document as column headers, and convert it to the following CSV file:

Student Name

Student ID


Student 1



Student 2



How To Read JSON Body Into CSV File By Using GET Method


The following should be installed in the system:

  • JDK

  • An IDE ( used eclipse here)

  • Maven

Step 1:

Create one Maven project in IDE and add the dependency for Jackson CSV data formatter in pom.xml file :






We'll also add the dependency for the core Jackson databind:






Note: Please use the latest version that's available in Maven Dependency

Step 2:

To read JSON and write to CSV

we'll use a combination of ObjectMapper and CSVMapper to convert between JSON and CSV. [ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON]

-> Create a package under the maven project in that create one TestNG class file

( ex: below is the code snippet

import static io.restassured.RestAssured.given;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.*;

public void Test_01() throws Exception {

// GET Method invoked and write the json response to .csv	
	  Response response = given().auth()
	.basic("username", "****").when() // enter valid credentials 
	.get("http://lms rest assured api/ "); // use valid api  
	  // We used Jackson's ObjectMapper to read our JSON document into a tree of JsonNode objects
		  JsonNode jsonTree = new ObjectMapper().readTree(response.asString());
		  //CsvSchema. This determines the column headers, types, and sequence of columns in the CSV file.
		  //To do this, we created a CsvSchema Builder and set the column headers to match the JSON field names
 CsvSchema.Builder csvSchemaBuilder = CsvSchema.builder();
JsonNode firstObject = jsonTree.elements().next();
		  firstObject.fieldNames().forEachRemaining(fieldName -> {csvSchemaBuilder.addColumn(fieldName);} );
		CsvSchema csvSchema =;
		  // We created a CsvMapper with our CsvSchema, and finally, we write the jsonTree to our CSV file
		  CsvMapper csvMapper = new CsvMapper();
	.writeValue(new File("src/test/resources/lmsget.csv"), jsonTree);

	  }	catch(Exception ex)

		  throw ex;

Note: Can be optimized to use the url's and file path's from application yml or config files

Step 3 :

a) Run the Test script and verify if the tests have passed or not.

b) Check weather .csv file is generated in the specified path location (e.g " src/test/resources/lmsget.csv" ) , where we can see all the data from the GET api response is saved in the .csv file. Refer below screenshot image

Helpful Notes :

REST Assured itself is a Domain Specific Language (DSL) for writing tests for RESTful web services and does not offer a mechanism for writing data driven tests (i.e., the ability to write a single test that can be executed multiple times with different sets of input and validation parameters). However, REST Assured tests are often combined with JUnit or TestNG, and the latter offers an easy to use mechanism to create data driven tests through the use of the DataProvider mechanism.

Hope this blog is helpful !!

In the next blog will see how to test the POST method by providing the request body reading from .csv data file and converting the JSON response from the POST api to .csv

1,596 views0 comments



bottom of page