Table of contents
1.
Introduction
2.
Dependencies Required to Compare Two JSON
3.
JsonNode equals method in Jackson API
4.
Comparing Two Json Objects using Jackson
5.
Comparing Two Json Arrays using Jackson
6.
Frequently Asked Questions
6.1.
What is REST API Testing?
6.2.
What is JSON?
6.3.
What is Jackson?
6.4.
Why are projects using Maven?
6.5.
What is client-server architecture?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

REST Assured – Compare Two JSON using Jackson – Java Library

Author Ayush Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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!

REST Assured – Compare Two JSON using Jackson – Java Library

Read more, Fibonacci Series in Java

Dependencies Required to Compare Two JSON

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.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.0-rc3</version>
</dependency>
You can also try this code with Online Java Compiler
Run Code

JsonNode equals method in Jackson API

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
Run Code


Output:

OUTPUT

Comparing Two Json Arrays using Jackson

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));
    }
}


Output:

output

Must Read Array of Objects in Java

Frequently Asked Questions

What is REST API Testing?

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: 

✴️ Rest Assured Setup

✴️ Rest Assured- Static Import in Java

✴️ Rest Assured- Writing Response in a Text File
 

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.

We wish you Good Luck! 

Happy Learning!

Live masterclass