LOADING

Type to search

Java REST Assured

JSON Dynamically – Multiple ways of creating payload

Share

First of this series, we looked into couple of ways in creating JSON dynamically.

Method 3: Using JSONObject and JSONArray.

Using JSONObject and JSONArray classes of org.json library, JSON object can be created. For this you need to add org.json library to your project. If you are on Maven project you can this library as a dependency (How to add dependency in maven).

#Version while writing this post
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>

First, let us look back at the JSON object we refer.

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

Now frame JSON object dynamically using JSONObject and JSONArray classes

JSONObject jsonObject = new JSONObject();

// Creating Name Object with first and last name
JSONObject nameObject = new JSONObject();
nameObject.put("firstname", "Testing");
nameObject.put("lastname", "Mag");
        
// Creating Address Object with required keys
JSONObject addressObject = new JSONObject();
addressObject.put("street", "");
addressObject.put("city", "");
addressObject.put("state", "");
addressObject.put("country", "");
    	    	
// Creating Role Array
JSONArray roleArray = new JSONArray();
roleArray.put("admin");
roleArray.put("contributor");
        
//Creating complete JSON Object
jsonObject.put("name", nameObject);
jsonObject.put("role", roleArray);
jsonObject.put("email","[email protected]");
jsonObject.put("address", addressObject);
jsonObject.put("mobile","");

Method 4: Using JSONObject and Map.

In another way you can create Map object and convert to JSON object. for this first create Map Object using HasMap class from Java.

Map<String, Object> map = new HashMap<String, Object>();
        
// Creating Name Object with first and last name
Map<String, Object> nameMap = new HashMap<String, Object>();
nameMap.put("firstname", "Testing");
nameMap.put("lastname", "Mag");
        
// Creating Address Object with required keys
Map<String, Object> addressMap = new HashMap<String, Object>();
addressMap.put("street", "");
addressMap.put("city", "");
addressMap.put("state", "");
addressMap.put("country", "");
        
map.put("name", nameMap);
map.put("role", Arrays.asList("admin","contributor"));
map.put("email","[email protected]");
map.put("address", addressMap);
map.put("mobile", "");
        
JSONObject json = new JSONObject(map);

Time elapsed for executing,

  • Method 3 : 5205700
  • Method 4 : 6591200

Now pass JSON object created using either method 3 or 4 in your POST request you want to test, payload.

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

when().body(jsonObject).

post("/endpoint").

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

There are other ways of creating JSON dynamically but they all are of least adopted ones. But notable among them is using POJO classes, you can use this online utility tool for the purpose

But the way of framing JSON by using VelocityEngine library from org.apache is one I want to highlight. Using the engine we can reading json template consisting keys and map data to form a JSON object.

Further we can look into this particular way of using VelocityEngine in another post.

Leave a Comment

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