Introduction
As a part of the End to End REST Assured Tutorial, in this post, we will learn to compare JSON objects using JSONassert library.
We may need to compare two JSONs 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 or similar or we just want to check the presence of some fields in another JSON then instead of writing some logic to assert them, we can directly compare with an existing JSON response.
We have already compared two JSONs using the Jackson library here. In this post, we will use JSONassert library for the same.
Required Dependency
I have used available below latest dependency of JSONassert. You can always get the latest dependency from Maven central repo.
org.skyscreamer jsonassert 1.5.0 test
Prerequisite post
You must go through the below post to understand about JSONassert Java library and different comparing modes.
Introduction To JsonAssert Library
Method assertEquals()
JSONassert provides multiple static overloaded assertEquals() method. We will see most of them.
Lenient mode
As we know from the previous post that in Lenient mode extensibility will be allowed and no strict ordering will be checked. Let’s see example programs of comparing JSONs.
Comparing two Exact same JSON Objects
assertEquals() method will pass as both JSON strings are the same.
@Test public void exactSameJson() throws JSONException { String jsoNobject1 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\"\r\n" + "}"; String jsonObject2 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\"\r\n" + "}"; // Lenient mode - extensible and no strict ordering JSONAssert.assertEquals(jsoNobject1, jsonObject2, JSONCompareMode.LENIENT); }
Comparing two JSONs with the same fields but different in order
In the below example, both JSON will have the same fields with the same values but the order of fields will not be the same. Since we are using LENIENT mode where strict order is not applicable. Anyway, strict order is meaningful for arrays not for JSON objects. So assertEquals() will pass.
@Test public void exactSameJsonWIthDifferentOrder() throws JSONException { String jsoNobject1 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\"\r\n" + "}"; String jsonObject2 = "{\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"firstName\": \"Amod\"\r\n" + "}"; // Lenient mode - extensible and no strict ordering but ordering will not be applicable for // JSON objects JSONAssert.assertEquals(jsoNobject1, jsonObject2, JSONCompareMode.LENIENT); }
Comparing two JSONs with the same fields but different values
In this scenario assertEquals() will fail as a value is not matching.
@Test public void sameFieldsWithDifferentValues() throws JSONException { String jsoNobject1 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\"\r\n" + "}"; String jsonObject2 = "{\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"firstName\": \"Rahul\"\r\n" + "}"; JSONAssert.assertEquals(jsoNobject1, jsonObject2, JSONCompareMode.LENIENT); }
Output
FAILED: sameFieldsWithDifferentValues java.lang.AssertionError: firstName Expected: Amod got: Rahul
Comparing two JSONs with the same fields but different datatype of values
This scenario is important and I am covering it to let you know that “18” and 18 both are not the same while comparing JSONs using JSONassert. Data types are different although they have the same value.
@Test public void unmatchedDataType() throws JSONException { String jsoNobject1 = "{\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"firstName\": \"Amod\",\r\n" + " \"age\": \"18\"\r\n" + "}"; String jsonObject2 = "{\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"firstName\": \"Amod\",\r\n" + " \"age\": 18\r\n" + "}"; // First json has 18 as string while second json has 18 as int JSONAssert.assertEquals(jsoNobject1, jsonObject2, JSONCompareMode.LENIENT); }
Output
FAILED: unmatchedDataType java.lang.AssertionError: age Expected: 18 got: 18
Strict Mode
If we do not want extensibility or we want to do an exact match of fields in both JSON then we can go for strict mode.
@Test public void strictMatchExample1() throws JSONException { String jsoNobject1 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\"\r\n" + "}"; String jsonObject2 = "{\r\n" + " \"firstName\" : \"Amod\",\r\n" + " \"lastName\": \"Mahajan\",\r\n" + " \"age\": 28\r\n" + "}"; JSONAssert.assertEquals(jsoNobject1, jsonObject2, JSONCompareMode.STRICT); }
Output
FAILED: strictMatchExample1 java.lang.AssertionError: Unexpected: age
Using overloaded assertEquals() method
As we can see in the above screenshot that we have multiple overloaded assertEquals() methods. We are passing JSON as a string. We can also pass JSONObject or JSONArray. They are just giving you multiple options to pass actual and expected JSONs.
package JsonObject; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; import org.testng.annotations.Test; public class LinentMatchWithJsonObject { @Test public void matchJsonObject() throws JSONException { JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("firstName", "Amod"); jsonObject1.put("lastName", "Mahajan"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("firstName", "Amod"); jsonObject2.put("lastName", "Mahajan"); JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT); } }
Instead of using JSONCompareMode enum, we can use another overloaded method assertEquals() which takes a boolean parameter to pass mode. By default, it will be non-strict and pass as true if we want strict mode.
assertEquals(JSONObject expected, JSONObject actual, boolean strict)
You can also print a custom message when JSONs are not identical.
package JsonObject; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.testng.annotations.Test; public class LinentMatchWithJsonObject { @Test public void matchJsonObject() throws JSONException { JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("firstName", "Amod"); jsonObject1.put("lastName", "Mahajan"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("firstName", "Rahul"); jsonObject2.put("lastName", "Mahajan"); //JSONAssert.assertEquals(jsonObject1, jsonObject2, false); JSONAssert.assertEquals("Jsons are not equal", jsonObject1, jsonObject2, false); } }
Output
FAILED: matchJsonObject java.lang.AssertionError: Jsons are not equal firstName Expected: Amod got: Rahul
Handling Error using try-catch
You must have observed that when comparison fails it throws AssertionError. You need to use the same assertion in a catch block or use its superclass “Error”.
package JsonObject; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.testng.annotations.Test; public class ErrorHandlingJSONassert { @Test public void errorHandling() throws JSONException { JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("firstName", "Amod"); jsonObject1.put("lastName", "Mahajan"); JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("firstName", "Rahul"); jsonObject2.put("lastName", "Mahajan"); try { JSONAssert.assertEquals(jsonObject1, jsonObject2, false); }catch(Error e) { System.out.println("Error occured as JSONs are not same."); } } }
Output
Error occured as JSONs are not same. PASSED: errorHandling
You can clone the git repo consisting above examples 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.