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
1 2 3 4 5 6 |
<!-- 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() 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
- The above method will return true if JSON nodes are equal.
- The order of root elements in JSON nodes will not matter.
- The order of elements in the JSON array will matter and it will not be the same.
Example Programs
Compare JSON Objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
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
1 2 3 |
true true true |
Compare JSON Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
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
1 2 |
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.
How to ignore few fields in the Json comparison. Example Date field.