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.
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.
Now create a <dependency> tag and paste the code given below.
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
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
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
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.
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 problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!