When I am writing tests to confirm the JSON structure generated in the API with the expected structure, Almost always I prefer to use the JSON path for this job but In some scenarios, you can’t use the JSON path and write one assertion for each field in the expected JSON result, for example when your API produces a very large JSON response. In these cases, maybe it is a good idea to have the…
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>
JsonNode equals() methodAn 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
Example ProgramsCompare 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
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)); } }
OutputYou 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 |