Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Rest Assured is a library of Java that allows users to verify and test the Rest APIs and Web Services. The Rest Assured-based libraries can validate the HTTP responses coming from the server.
In this blog, we will discuss how to compare two Json using Jackson dependencies of Java library in Rest assured. Let’s start going!
In order to compare two JSON using Jackson in Rest Assured, we need to add the below given Jackson databind dependencies into the pom.xml of your maven project.
The Jackson API abstract class JsonNode offers an equals() abstract method that can be used to compare node objects. The ObjectNode and ArrayNode classes implement the equals() method, an abstract method.
Full (deep) value equality is used to compare the equality of root nodes allowing for the comparison of entire JSON trees for equality.
Before calling the equal() method, we need to convert the given JSON to ObjectNode or ArrayNode.
The following point must be remembered while using the equals() method to compare two json using jackson:-
🚀 If JSON nodes are equal, the method mentioned above will return true.
🚀 JSON nodes' root elements' order is not important.
🚀 The JSON array's element order will differ and will be affected by it.
Comparing Two Json Objects using Jackson
The JsonNode.equals() method is used to compare the two JSON Objects. Let us understand the comparison of the two JSON objects with the help of an example.
Example:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class sample {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException
{
// Creating Object1
String jOb1 = "{\r\n" +
" \"first_Name\": \"Harshit\",\r\n" +
" \"last_Name\": \"Tripathi\",\r\n" +
" \"address\": {\r\n" +
" \"city\": \"Delhi\",\r\n" +
" \"state\": \"India\"\r\n" +
" }\r\n" +
"}";
// Creating Object2
String jOb2 = "{\r\n" +
" \"first_Name\": \"Harshit\",\r\n" +
" \"last_Name\": \"Tripathi\",\r\n" +
" \"address\": {\r\n" +
" \"city\": \"Delhi\",\r\n" +
" \"state\": \"India\"\r\n" +
" }\r\n" +
"}";
// Creating an object of ObjectMapper to read the objects in Json
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode1 = objectMapper.readTree(jOb1);
JsonNode jsonNode2 = objectMapper.readTree(jOb2);
// Compare two json using Jackson and displaying the result
System.out.println(jsonNode1.equals(jsonNode2));
}
}
You can also try this code with Online Java Compiler
We will compare two JSON arrays using JsonNode.equals() method in the same way we compare two JSON Objects. Let us understand the comparison of the two JSON arrays with the help of an example.
Example:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class sample {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
// Creating Array1
String jsonArray1 = "[\r\n" +
" {\r\n" +
" \"first_Name\": \"Ram\",\r\n" +
" \"last_Name\": \"Tripathi\",\r\n" +
" \"address\": {\r\n" +
" \"city\": \"Mumbai\",\r\n" +
" \"country\": \"India\"\r\n" +
" }\r\n" +
" } \r\n" +
"]";
// Creating Array2
String jsonArray2 = "[\r\n" +
" {\r\n" +
" \"first_Name\": \"Ram\",\r\n" +
" \"last_Name\": \"Tripathi\",\r\n" +
" \"address\": {\r\n" +
" \"city\": \"Mumbai\",\r\n" +
" \"country\": \"India\"\r\n" +
" }\r\n" +
" } \r\n" +
"]";
// Creating an object of ObjectMapper to read the Array in Json
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode1 = objectMapper.readTree(jsonArray1);
JsonNode jsonNode2 = objectMapper.readTree(jsonArray2);
// Compare two json using Jackson and displaying the result
System.out.println(jsonNode1.equals(jsonNode2));
}
}
An open-source method for testing RESTful APIs for web applications is called REST API testing. It is mostly used to test web applications that use JSON and XML. All methods, including GET, PUT, POST, PATCH, and DELETE, are compatible. In Java, the REST API is a library.
What is JSON?
JSON stands for JavaScript Object Notation. It originated from JavaScript. It is an open standard data-interchange format that is easy to read and write.
What is Jackson?
A fast JSON processor for Java is called Jackson. The most widely used library for serializing Java objects or maps to JSON and vice-versa. It depends on Java.
Why are projects using Maven?
Maven mainly helps download dependencies for Java-based applications, such as libraries or JAR files.
What is client-server architecture?
Using the client-server model, a server offers resources and services to one or more clients. Mail servers, web servers, and file servers are a few types. As a result, the server grants the request made by the client.
Conclusion
Congratulations on finishing the blog! We have discussed how to compare two json using Jackson dependencies of the Java library in Rest assured. We have further discussed the comparison of two JSON objects and arrays using Jackson.
We hope this blog has helped you enhance your knowledge of how to compare two json using Jackson in Rest Assured. Do not stop learning! We recommend you read some of our Rest Assured 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.