How to compare two json objects in java using jackson

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.

Required Dependency

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

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.12.1</version>

JsonNode equals() method

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.

Important points to be noted

  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.

Example Programs

Compare JSON Objects

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

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 {

ObjectMapper objectMapper;

public void compareTwoJsonObjects() throws JsonMappingException, JsonProcessingException

"  \"firstName\" : \"Amod\",\r\n" +

"  \"lastName\" : \"Mahajan\"\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));

public void compareTwoJsonObjectsWithDifferentOrderOfRootElements() throws JsonMappingException, JsonProcessingException

// Change in order of elements does not impact

"  \"firstName\" : \"Amod\",\r\n" +

"  \"lastName\" : \"Mahajan\"\r\n" +

"  \"lastName\" : \"Mahajan\",\r\n" +

"  \"firstName\" : \"Amod\"\r\n" +

jsonNode1 = objectMapper.readTree(JsonObject1);

jsonNode2 = objectMapper.readTree(jsonObject2);

System.out.println(jsonNode1.equals(jsonNode2));

public void compareTwoNestedJsonObjects() throws JsonMappingException, JsonProcessingException

// Nested json objects can also be compared with equals method

"  \"lastName\": \"Mahajan\",\r\n" +

"  \"firstName\": \"Amod\",\r\n" +

"    \"city\": \"Katihar\",\r\n" +

"    \"state\": \"Bihar\"\r\n" +

"  \"lastName\": \"Mahajan\",\r\n" +

"  \"firstName\": \"Amod\",\r\n" +

"    \"city\": \"Katihar\",\r\n" +

"    \"state\": \"Bihar\"\r\n" +

jsonNode1 = objectMapper.readTree(JsonObject1);

jsonNode2 = objectMapper.readTree(jsonObject2);

System.out.println(jsonNode1.equals(jsonNode2));

Output

Compare JSON Arrays

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

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 {

ObjectMapper objectMapper;

public void compareTwoJsonArrays() throws JsonMappingException, JsonProcessingException

"    \"lastName\": \"Mahajan\",\r\n" +

"    \"firstName\": \"Amod\",\r\n" +

"      \"city\": \"Katihar\",\r\n" +

"      \"state\": \"Bihar\"\r\n" +

"    \"lastName\": \"Animesh\",\r\n" +

"    \"firstName\": \"Prashant\",\r\n" +

"      \"city\": \"Kolkata\",\r\n" +

"      \"state\": \"WB\"\r\n" +

"    \"lastName\": \"Mahajan\",\r\n" +

"    \"firstName\": \"Amod\",\r\n" +

"      \"city\": \"Katihar\",\r\n" +

"      \"state\": \"Bihar\"\r\n" +

"    \"lastName\": \"Animesh\",\r\n" +

"    \"firstName\": \"Prashant\",\r\n" +

"      \"city\": \"Kolkata\",\r\n" +

"      \"state\": \"WB\"\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));

public void compareTwoJsonArraysWithDifferentOrderOfElements() throws JsonMappingException, JsonProcessingException

// Change in order of elements in array will impact and it will not be considered same

"    \"lastName\": \"Mahajan\",\r\n" +

"    \"firstName\": \"Amod\",\r\n" +

"      \"city\": \"Katihar\",\r\n" +

"      \"state\": \"Bihar\"\r\n" +

"    \"lastName\": \"Animesh\",\r\n" +

"    \"firstName\": \"Prashant\",\r\n" +

"      \"city\": \"Kolkata\",\r\n" +

"      \"state\": \"WB\"\r\n" +

"    \"lastName\": \"Animesh\",\r\n" +

"    \"firstName\": \"Prashant\",\r\n" +

"      \"city\": \"Kolkata\",\r\n" +

"      \"state\": \"WB\"\r\n" +

"    \"lastName\": \"Mahajan\",\r\n" +

"    \"firstName\": \"Amod\",\r\n" +

"      \"city\": \"Katihar\",\r\n" +

"      \"state\": \"Bihar\"\r\n" +

jsonNode1 = objectMapper.readTree(jsonArray1);

jsonNode2 = objectMapper.readTree(jsonArray2);

System.out.println(jsonNode1.equals(jsonNode2));

Output

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Skip back to main navigation