REST Assured Tutorial 57 – Editing Existing JSON Object On The Fly Using JsonNode – Jackson

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will edit or customize existing JSON object on the fly using JsonNode of Jackson API.

Many times we do not want to create POJO classes and we may need to edit an existing JSON quickly as per requirement. In that case, JsonNode provided by Jackson API is really helpful.

Prerequisite

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-



    com.fasterxml.jackson.core
    jackson-databind
    2.11.3

You must go through with below posts first to understand this post better:-

How To Create A JSON Object Using Jackson API – ObjectMapper – CreateObjectNode()

How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()

Editing JSON using JsonNode

Adding a new key with a primitive value to existing JSON

Suppose we have an existing JSON as below:-

{
  "firstname": "Amod",
  "lastname": "Mahajan"
}

Let’s add a new field “role” with value as “admin“.

We will deserialize the above JSON object as ObjectNode as given JSON is a JSON Object. Please note here that the superclass of ObjectNode is JsonNode.

package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class EditJsonObjectOnFly {

	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
				"  \"firstname\": \"Amod\",\r\n" + 
				"  \"lastname\": \"Mahajan\"\r\n" + 
				"}";

		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		objectNode.put("role", "admin");
		System.out.println(objectNode.toPrettyString());

	}

}

Output

{
  "firstname" : "Amod",
  "lastname" : "Mahajan",
  "role" : "admin"
}

Adding a new key with a JSON object value to an existing JSON

Let’s add a new key with value as a JSON object. For example – Adding a new field “bookingDetails” with the below value. Basically, we need to add an ObjectNode and the post link under the Prerequisite section explains that concept.

"bookingDetails" : {
    "firstname" : "Jim",
    "lastname" : "Brown"
}
package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class EditJsonObjectOnFly2 {

	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
				"  \"firstname\": \"Amod\",\r\n" + 
				"  \"lastname\": \"Mahajan\"\r\n" + 
				"}";

		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		ObjectNode bookingDetails = objectMapper.createObjectNode();
		bookingDetails.put("firstname", "Jim");
		bookingDetails.put("lastname", "Brown");

		objectNode.set("bookingDetails", bookingDetails);
		System.out.println(objectNode.toPrettyString());

	}

}

Output

{
  "firstname" : "Amod",
  "lastname" : "Mahajan",
  "bookingDetails" : {
    "firstname" : "Jim",
    "lastname" : "Brown"
  }
}

Adding a new key inside a nested JSON Object

In the output of the above program, suppose we need to add another key inside “bookingDetails” key. I.e final output should be as below:-

"bookingDetails" : {
    "firstname" : "Jim",
    "lastname" : "Brown",
    "gender" : "male"
  }
package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class EditJsonObjectOnFly3 {

	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
				"  \"firstname\" : \"Amod\",\r\n" + 
				"  \"lastname\" : \"Mahajan\",\r\n" + 
				"  \"bookingDetails\" : {\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\"\r\n" + 
				"  }\r\n" + 
				"}";

		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.with("bookingDetails").put("gender", "male");
		System.out.println(objectNode.toPrettyString());

	}

}

Update value of existing keys

We can also modify the value of existing keys.

package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class EditJsonObjectOnFly4 {

	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
				"  \"firstname\" : \"Amod\",\r\n" + 
				"  \"lastname\" : \"Mahajan\",\r\n" + 
				"  \"bookingDetails\" : {\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\"\r\n" + 
				"  }\r\n" + 
				"}";

		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.put("firstname", "Animesh");
		objectNode.with("bookingDetails").put("firstname", "Rahul");
		System.out.println(objectNode.toPrettyString());
	}

}

Output

{
  "firstname" : "Animesh",
  "lastname" : "Mahajan",
  "bookingDetails" : {
    "firstname" : "Rahul",
    "lastname" : "Brown"
  }
}

Remove or delete existing key

We can also remove keys from a given JSON Object.

package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class EditJsonObjectOnFly5 {

	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + "  \"firstname\" : \"Amod\",\r\n" + "  \"lastname\" : \"Mahajan\",\r\n"
				+ "  \"bookingDetails\" : {\r\n" + "    \"firstname\" : \"Jim\",\r\n"
				+ "    \"lastname\" : \"Brown\"\r\n" + "  }\r\n" + "}";

		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);

		objectNode.remove("firstname");
		objectNode.with("bookingDetails").remove("firstname");

		System.out.println(objectNode.toPrettyString());

	}

}

Output

{
  "lastname" : "Mahajan",
  "bookingDetails" : {
    "lastname" : "Brown"
  }
}

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

1 thought on “REST Assured Tutorial 57 – Editing Existing JSON Object On The Fly Using JsonNode – Jackson

Leave a Reply

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