Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 68 – Compare Two JSON using Jackson – Java Library

Posted on 02/19/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn to compare two JSON using the Jackson library.

We may need to compare two JSON during API testing. For example – If we are going to get the same JSON response for an API every time or some parts of the response are always constant then instead of writing some logic to assert them, we can directly compare with an existing JSON response.

We have a couple of good Java libraries to do. In this post, we will use Jackson Library to compare two JSON responses.

  com.fasterxml.jackson.core jackson-databind 2.12.1

An abstract class JsonNode provided by Jackson API provides an abstract method called equals() which can be used to compare node objects. Equality for node objects is defined as a full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing the equality of root nodes. equals() method is an abstract method and implemented by ObjectNode and ArrayNode classes.

We need to convert the given JSON to JsonNode ( or ObjectNode or ArrayNode) first and then we can call the equals method on it.

  1. The above method will return true if JSON nodes are equal.
  2. The order of root elements in JSON nodes will not matter.
  3. The order of elements in the JSON array will matter and it will not be the same.
package CompareJSONUsingJackson;

import org.testng.annotations.Test;

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 CompareJsonObjects {
        
        String JsonObject1;
        String jsonObject2;
        ObjectMapper objectMapper;
        JsonNode jsonNode1;
        JsonNode jsonNode2;
        
        @Test
        public void compareTwoJsonObjects() throws JsonMappingException, JsonProcessingException
        {
                JsonObject1 = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\"\r\n" + 
                                "}";
                
                jsonObject2 = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\"\r\n" + 
                                "}";
                
                objectMapper = new ObjectMapper();
                jsonNode1 = objectMapper.readTree(JsonObject1);
                jsonNode2 = objectMapper.readTree(jsonObject2);
                
                // Checking if both json objects are same
                System.out.println(jsonNode1.equals(jsonNode2));
        }
        
        @Test
        public void compareTwoJsonObjectsWithDifferentOrderOfRootElements() throws JsonMappingException, JsonProcessingException
        {
                // Change in order of elements does not impact
                JsonObject1 = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\"\r\n" + 
                                "}";
                
                jsonObject2 = "{\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"firstName\" : \"Amod\"\r\n" + 
                                "  \r\n" + 
                                "}";
                
                jsonNode1 = objectMapper.readTree(JsonObject1);
                jsonNode2 = objectMapper.readTree(jsonObject2);
                
                System.out.println(jsonNode1.equals(jsonNode2));
        }
        
        @Test
        public void compareTwoNestedJsonObjects() throws JsonMappingException, JsonProcessingException
        {
                
                // Nested json objects can also be compared with equals method
                JsonObject1 = "{\r\n" + 
                                "  \"lastName\": \"Mahajan\",\r\n" + 
                                "  \"firstName\": \"Amod\",\r\n" + 
                                "  \"address\": {\r\n" + 
                                "    \"city\": \"Katihar\",\r\n" + 
                                "    \"state\": \"Bihar\"\r\n" + 
                                "  }\r\n" + 
                                "}";
                jsonObject2 = "{\r\n" + 
                                "  \"lastName\": \"Mahajan\",\r\n" + 
                                "  \"firstName\": \"Amod\",\r\n" + 
                                "  \"address\": {\r\n" + 
                                "    \"city\": \"Katihar\",\r\n" + 
                                "    \"state\": \"Bihar\"\r\n" + 
                                "  }\r\n" + 
                                "}";
                
                jsonNode1 = objectMapper.readTree(JsonObject1);
                jsonNode2 = objectMapper.readTree(jsonObject2);
                
                System.out.println(jsonNode1.equals(jsonNode2));
                
        }

}
true
true
true
package CompareJSONUsingJackson;

import org.testng.annotations.Test;

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 CompareJsonArrays {
        
        String jsonArray1;
        String jsonArray2;
        ObjectMapper objectMapper;
        JsonNode jsonNode1;
        JsonNode jsonNode2;
        
        @Test
        public void compareTwoJsonArrays() throws JsonMappingException, JsonProcessingException
        {
                jsonArray1 = "[\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Mahajan\",\r\n" + 
                                "    \"firstName\": \"Amod\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Katihar\",\r\n" + 
                                "      \"state\": \"Bihar\"\r\n" + 
                                "    }\r\n" + 
                                "  },\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Animesh\",\r\n" + 
                                "    \"firstName\": \"Prashant\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Kolkata\",\r\n" + 
                                "      \"state\": \"WB\"\r\n" + 
                                "    }\r\n" + 
                                "  }\r\n" + 
                                "]";
                
                jsonArray2 = "[\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Mahajan\",\r\n" + 
                                "    \"firstName\": \"Amod\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Katihar\",\r\n" + 
                                "      \"state\": \"Bihar\"\r\n" + 
                                "    }\r\n" + 
                                "  },\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Animesh\",\r\n" + 
                                "    \"firstName\": \"Prashant\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Kolkata\",\r\n" + 
                                "      \"state\": \"WB\"\r\n" + 
                                "    }\r\n" + 
                                "  }\r\n" + 
                                "]";
                
                objectMapper = new ObjectMapper();
                jsonNode1 = objectMapper.readTree(jsonArray1);
                jsonNode2 = objectMapper.readTree(jsonArray2);
                
                // Checking if both json objects are same
                System.out.println(jsonNode1.equals(jsonNode2));
        }
        
        @Test
        public void compareTwoJsonArraysWithDifferentOrderOfElements() throws JsonMappingException, JsonProcessingException
        {
                
                // Change in order of elements in array will impact and it will not be considered same
                jsonArray1 = "[\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Mahajan\",\r\n" + 
                                "    \"firstName\": \"Amod\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Katihar\",\r\n" + 
                                "      \"state\": \"Bihar\"\r\n" + 
                                "    }\r\n" + 
                                "  },\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Animesh\",\r\n" + 
                                "    \"firstName\": \"Prashant\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Kolkata\",\r\n" + 
                                "      \"state\": \"WB\"\r\n" + 
                                "    }\r\n" + 
                                "  }\r\n" + 
                                "]";
                
                jsonArray2 = "[\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Animesh\",\r\n" + 
                                "    \"firstName\": \"Prashant\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Kolkata\",\r\n" + 
                                "      \"state\": \"WB\"\r\n" + 
                                "    }\r\n" + 
                                "  },\r\n" + 
                                "  {\r\n" + 
                                "    \"lastName\": \"Mahajan\",\r\n" + 
                                "    \"firstName\": \"Amod\",\r\n" + 
                                "    \"address\": {\r\n" + 
                                "      \"city\": \"Katihar\",\r\n" + 
                                "      \"state\": \"Bihar\"\r\n" + 
                                "    }\r\n" + 
                                "  }\r\n" + 
                                "]";
                
                jsonNode1 = objectMapper.readTree(jsonArray1);
                jsonNode2 = objectMapper.readTree(jsonArray2);
                
                System.out.println(jsonNode1.equals(jsonNode2));
                
                
        }

}
true
false

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

Uncategorized

Post navigation

Previous Post: FindAll Annotation In Page Factory In Selenium WebDriver
Next Post: Selenium Interview Experience

Related Posts

Postman Tutorial Part 45 – Data Driven Testing in Postman Using JSON File Uncategorized
November 16, 2018 – Make Selenium Easy Uncategorized
November 3, 2018 – Make Selenium Easy Uncategorized
xpathYatra – Make Selenium Easy Uncategorized
git reset Uncategorized
September 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark