Table of contents
1.
Introduction
2.
Prerequisites
3.
Editing JSON using JsonNode
3.1.
Primitive Value
3.2.
JSON Object
3.3.
Nested JSON Object
3.4.
Update the Value
3.5.
Remove Existing Key
4.
Frequently Asked Questions
4.1.
Why is JSON used over XML?
4.2.
What is the main difference between POST, PUSH, and PATCH in REST Assured?
4.3.
Can we test the SOAP APIs in REST Assured?
4.4.
Do we need to buy the license for REST Assured?
4.5.
What is the difference between REST Assured and REST?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

REST Assured - Editing Existing JSON Object On The Fly Using JsonNode - Jackson

Author Sagar Mishra
0 upvote

Introduction

In a world full of Web Apps, it is necessary to provide bug-free software to the user for a better experience. This motivated DevOps to test their apps at an earlier stage called API Testing. Now, there are two types of API Testing, Manual and Automation. We can perform Manual API Testing using Postman. And for API Testing Automation, we can use a library in Java called Rest Assured.

REST Assured - Editing Existing JSON Object On The Fly Using JsonNode - Jackson

Today we will learn an important topic in Rest Assured, i.e., Editing JSON Object On The Fly Using JsonNode - Jackson. Let's start the blog without wasting time. 

Prerequisites

There are some steps to follow before moving to the main topic.

Go to the pom.xml file inside your package.

Prerequisites

Now create a <dependency> tag and paste the code given below.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.3</version>
</dependency>


It is suggested to go through the blog How To Create a JSON Object and array Using Jackson API before moving ahead. It will help you to understand this in a better way. 

Editing JSON using JsonNode

We will see four examples of editing the existing JSON object using JsonNode.

Primitive Value

In the "Editing Existing JSON Object On The Fly Using JsonNode" series, our first topic is to add a new key with a primitive value to the existing JSON.

Let's say there is an existing JSON, and we have to add a new field with a value. The existing JSON is as follows.

{
  "firstname": "Sagar",
  "lastname": "Mishra"
}


Now let's check the working code to do the above task. 

package codingninjas;

import org.junit.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 ninjaTest {
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
		"  \"firstname\": \"Sagar\",\r\n" + 
		"  \"lastname\": \"Mishra\"\r\n" + 
		"}";
		
		
		ObjectMapper objectMapper = new ObjectMapper();
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		objectNode.put("role", "admin");
		
		// Printing the output in the Editing Existing JSON Object On The Fly Using JsonNode series
		System.out.println(objectNode.toPrettyString());
	}
}
You can also try this code with Online Java Compiler
Run Code


Output:

Output

JSON Object

In the "Editing Existing JSON Object On The Fly Using JsonNode" series, our next topic is to add a new key with a JSON object to an existing JSON.

So, let's try to add a new key with JSON object value to an existing JSON.

We will add a new field called Flight Booking Details, including the details below.

  • Flight name
  • Takeoff
  • Destination 
  • Date of travel
     

We will add an ObjectNode to perform the above task. You can refer to the prerequisite links for the concept explanation. Now let's check out the code.

package codingninjas;

import org.junit.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 ninjaTest {
		@Test
		public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
			String jsonObject = "{\r\n" + 
			"  \"firstname\": \"Sagar\",\r\n" + 
			"  \"lastname\": \"Mishra\"\r\n" + 
			"}";
			 
			ObjectMapper objectMapper = new ObjectMapper();
			ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
			
			ObjectNode flightBooking = objectMapper.createObjectNode();
			flightBooking.put("flightname", "Air India");
			flightBooking.put("from", "Delhi");
			flightBooking.put("to", "Mumbai");
			flightBooking.put("date", "22-12-2022");
			 
			objectNode.set("Flight Booking Details", flightBooking);
			
			// Printing the output in the Editing Existing JSON Object On The Fly Using JsonNode series
			System.out.println(objectNode.toPrettyString());
		}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Nested JSON Object

In the "Editing Existing JSON Object On The Fly Using JsonNode" series, our next topic is to add a new key inside a nested JSON Object.

Let's say we already have a given nested JSON Object. Our task is to add a new key with its value in the JSON Object. The existing JSON is as follows.

{
  "firstname" : "Amod",
  "lastname" : "Mahajan",
  "Flight Booking Details" : {
    "flightname" : "Air India",
    "date" : "22-12-2022"
  }
} 


Now we will try to add the flight's departure time in the same JSON Object. Let's move to code for the solution.

package codingninjas;

import org.junit.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 ninjaTest {
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
		"  \"firstname\" : \"Sagar\",\r\n" + 
		"  \"lastname\" : \"Mishra\",\r\n" + 
		"  \"Flight Booking Details\" : {\r\n" + 
		"    \"flightname\" : \"Air India\",\r\n" + 
		"    \"date\" : \"22-12-2022\"\r\n" +
		"  }\r\n" + 
		"}";
		 
		ObjectMapper objectMapper = new ObjectMapper();
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.with("Flight Booking Details").put("time", "10:30PM");
		
		// Printing the output in the Editing Existing JSON Object On The Fly Using JsonNode series
		System.out.println(objectNode.toPrettyString()); 
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Update the Value

In the "Editing Existing JSON Object On The Fly Using JsonNode" series, our next topic is to update the value of existing keys.

We can also update the values of any existing keys. We will change the first name and the flight name in the output of the above program. Let's understand this with an example.

package codingninjas;

import org.junit.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 ninjaTest {
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		String jsonObject = "{\r\n" + 
		"  \"firstname\" : \"Sagar\",\r\n" + 
		"  \"lastname\" : \"Mishra\",\r\n" + 
		"  \"Flight Booking Details\" : {\r\n" + 
		"    \"flightname\" : \"Air India\",\r\n" + 
		"    \"date\" : \"22-12-2022\"\r\n" +
		"  }\r\n" + 
		"}";
		 
		ObjectMapper objectMapper = new ObjectMapper();
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.put("firstname", "Ashish");
		objectNode.with("Flight Booking Details").put("flightname", "SpiceJet");
		
		// Printing the output in the Editing Existing JSON Object On The Fly Using JsonNode series
		System.out.println(objectNode.toPrettyString());
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Remove Existing Key

In the "Editing Existing JSON Object On The Fly Using JsonNode" series, our last topic is to remove or delete the existing key.

We can also remove any key from the existing JSON Object. Let's try to remove the last name and travel date from our example.

package codingninjas;

import org.junit.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 ninjaTest {
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
		
		String jsonObject = "{\r\n" + 
		"  \"firstname\" : \"Sagar\",\r\n" + 
		"  \"lastname\" : \"Mishra\",\r\n" + 
		"  \"Flight Booking Details\" : {\r\n" + 
		"    \"flightname\" : \"Air India\",\r\n" + 
		"    \"date\" : \"22-12-2022\"\r\n" +
		"  }\r\n" + 
		"}";
		 
		ObjectMapper objectMapper = new ObjectMapper();
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.remove("lastname");
		objectNode.with("Flight Booking Details").remove("date");
		
		// Printing the output in the Editing Existing JSON Object On The Fly Using JsonNode series
		System.out.println(objectNode.toPrettyString());
	}
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Output

Must Read Array of Objects in Java.

Frequently Asked Questions

Why is JSON used over XML?

JSON is designed for data interchange. Thus it is faster than XML. Moreover, JSON can be parsed by a JavaScript function. At the same time, XML requires an XML parser.

What is the main difference between POST, PUSH, and PATCH in REST Assured?

POST is used for creating a resource (it doesn't matter if it was duplicated). PUT is for checking if the resource exists and then updating it. Else create a new resource. PATCH is always for updating a resource. 

Can we test the SOAP APIs in REST Assured?

We can test, but REST Assured does not explicitly support SOAP APIs testing.

Do we need to buy the license for REST Assured?

REST Assured is an open-source API testing tool, so don't worry about the license.

What is the difference between REST Assured and REST?

REST means REpresentation State Transfer. It is a set of protocols laid forward for developing APIs. On the other hand, REST Assured is a Java library that is used to write and manage test cases for REST APIs.

Conclusion

This article discusses editing existing JSON Objects on the fly using JsonNode. In detail, we have seen the four examples of editing the existing JSON object using JsonNode.

We hope this blog has helped you enhance your knowledge of editing existing JSON Objects on the fly using JsonNode. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass