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

Introduction

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



    com.fasterxml.jackson.core
    jackson-databind
    2.12.1

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

}
Output
true
true
true

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

}
Output
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

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.

1 thought on “REST Assured Tutorial 68 – Compare Two JSON using Jackson – Java Library”

Leave a Reply

Your email address will not be published. Required fields are marked *