LOADING

Type to search

Java REST Assured

Dynamic JSON – Multiple ways of creating payload

Share

In this post we would look into multiple ways of creating a dynamic JSON payload. Payload to be sent while testing POST & PUT and few instances of GET requests of your API services.

As an initial step we could look into simple JSON, once you get hold of concepts then you can move on to complex JSON’s.

JSON is a data representation using a Key Value pair separated by a colon (:). JSON object is always enclosed between {}, Similarly, data enclosed with [] is a JSON array. You can read more about JSON here.

For Instance, in this post we would use below JSON as reference.

{
  "name": {
    "firstname": "Testing",
    "lastname": "Mag"
  },
  "role":[
    "admin",
    "contributor"],
  "email": "admin@testingmag.in",
  "address": {
    "street": "",
    "city": "",
    "state": "",
    "country": ""
  },
  "mobile": ""
}

Method 1: Passign payload as String.

This is the most easiest way to create a payload, you simply need to pass on the dynamic JSON payload as a string.

For Example: You need to store complete JSON object as a String variable or read it as a String if you store in an external file. The Data Driven approach of reading values stored in external files can be looked in another post.

String jsonObject= "{\r\n" + 
"  \"name\": {\r\n" + 
"    \"firstname\": \"Testing\",\r\n" + 
"    \"lastname\": \"Mag\"\r\n" + 
"  },\r\n" + 
"  \"role\":[\r\n" + 
"    \"admin\",\r\n" + 
"    \"contributor\"],\r\n" + 
"  \"email\": \"admin@testingmag.in\",\r\n" + 
"  \"address\": {\r\n" + 
"    \"street\": \"\",\r\n" + 
"    \"city\": \"\",\r\n" + 
"    \"state\": \"\",\r\n" + 
"    \"country\": \"\"\r\n" + 
"  },\r\n" + 
"  \"mobile\": \"\"\r\n" + 
"}";

Then pass on the String variable in your request body.

Response response = given().contentType(ContentType.JSON).

when().body(jsonObject).

post("/endpoint").

then().extract().response();

Method 2: Payload as a File or Byte Array.

Keep JSON object data in a file, then you can use any of below methods to pass JSON object

  • Read file using File class of Java
  • Content of the file as Byte array.

Save our reference JSON object in a file with name sample.json.

Using File class:

File file = new File("<path to file location>/sample.json");

In this case your post request would look like,

Response response = given().contentType(ContentType.JSON).

when().body(file).

post("/endpoint").

then().extract().response();

Using Byte array,

byte[] jsonByteArray = Files.readAllBytes(Paths.get("<path to file>/myjson.json"));

In this case your post request would look as,

Response response = given().contentType(ContentType.JSON).

when().body(jsonByteArray).

post("/endpoint").

then().extract().response();

We can look how to get a file location here.

Still, there are other ways to create dynamic JSON payload which can be looked in Part 2 of this series.

Please provide your inputs which can help tester community gain more knowledge on this topic.

Leave a Comment

Your email address will not be published. Required fields are marked *