Introduction
As a part of the End to End REST Assured Tutorial, in this post, we will learn to compare the portion of JSON arrays and Objects using the JSONassert library.
We have already learned to Compare two JSON Objects and Compare two JSON Arrays using JSONassert library as a whole.
Many times we need to compare specific portions of JSON Objects or JSON Arrays. For example – Suppose we create a new booking and get booking details. When we fetch all active bookings then we just need to verify booking details of newly booking in that active list.
Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.
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, different comparing modes, and asserting JSON Objects.
Introduction To JsonAssert Library
Compare JSON Objects Using JSONassert Library
Compare JSON Arrays Using JSONassert Library
Compare portions of JSON Objects
JSON Object 1
{ "lastName": "Animesh", "firstName": "Prashant", "address": { "city": "Kolkata", "state": "WB" } }
JSON Object 2
{ "lastName": "Animesh", "firstName": "Prashant", "communicationAddress": { "city": "Kolkata", "state": "WB" }, "skills": [ "CA", "BCOM" ] }
We need to compare that the “address” object in the first JSON Object has the same values for keys as “communicationAddress” in the second JSON Object. In this case, we just need to use the overloaded assertEquals() method with some methods with JsonPath to fetch the desired portion of JSON objects.
Example Program
package ComparePortionOfJson; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; public class ComparePortionOfJsonObjects { public static void main(String[] args) throws JSONException { JSONObject jsonObject1 = new JSONObject("{\r\n" + " \"lastName\": \"Animesh\",\r\n" + " \"firstName\": \"Prashant\",\r\n" + " \"address\": {\r\n" + " \"city\": \"Kolkata\",\r\n" + " \"state\": \"WB\"\r\n" + " }\r\n" + "}"); JSONObject jsonObject2 = new JSONObject("{\r\n" + " \"lastName\": \"Animesh\",\r\n" + " \"firstName\": \"Prashant\",\r\n" + " \"communicationAddress\": {\r\n" + " \"city\": \"Kolkata\",\r\n" + " \"state\": \"WB\"\r\n" + " },\r\n" + " \"skills\": [\r\n" + " \"CA\",\r\n" + " \"BCOM\"\r\n" + " ]\r\n" + "}"); // I want to assert address objects are same in both // Since "address" and "communicationAddress" are JSON Objects so we need to use proper method i.e. getJSONObject() JSONAssert.assertEquals(jsonObject1.getJSONObject("address"), jsonObject2.getJSONObject("communicationAddress"), JSONCompareMode.LENIENT); } }
Compare portions of JSON Arrays
JSON Array 1
[ { "id": 1, "first_name": "Giavani", "last_name": "Skellorne" }, { "id": 2, "first_name": "Patton", "last_name": "Buchett" } ]
JSON Array 2
[ { "id": 1, "first_name": "Giavani", "last_name": "Skellorne" }, { "id": 2, "first_name": "Harland", "last_name": "Brookwood" }, { "id": 3, "first_name": "Leigh", "last_name": "Gatteridge" } ]
In the above JSON Arrays, we just want to compare objects at the first index.
Example Program
package ComparePortionOfJson; import org.json.JSONArray; import org.json.JSONException; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; public class ComparePortionOfJsonArrays { public static void main(String[] args) throws JSONException { JSONArray jSONArray1 = new JSONArray("[\r\n" + " {\r\n" + " \"id\": 1,\r\n" + " \"first_name\": \"Giavani\",\r\n" + " \"last_name\": \"Skellorne\"\r\n" + " },\r\n" + " {\r\n" + " \"id\": 2,\r\n" + " \"first_name\": \"Patton\",\r\n" + " \"last_name\": \"Buchett\"\r\n" + " }\r\n" + "]"); JSONArray jSONArray2 = new JSONArray("[\r\n" + " {\r\n" + " \"id\": 1,\r\n" + " \"first_name\": \"Giavani\",\r\n" + " \"last_name\": \"Skellorne\"\r\n" + " },\r\n" + " {\r\n" + " \"id\": 2,\r\n" + " \"first_name\": \"Harland\",\r\n" + " \"last_name\": \"Brookwood\"\r\n" + " },\r\n" + " {\r\n" + " \"id\": 3,\r\n" + " \"first_name\": \"Leigh\",\r\n" + " \"last_name\": \"Gatteridge\"\r\n" + " }\r\n" + "]"); // I want to assert address objects are same in both // Since index elements in both arrays are JSON objects so using proper method i.e. getJSONObject JSONAssert.assertEquals(jSONArray1.getJSONObject(0), jSONArray2.getJSONObject(0), JSONCompareMode.LENIENT); } }
You can also compare a portion of the JSON object to the portion of JSON Array. But remember you can not compare if both are of the not the same type. That will through compilation error.
ou 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.