Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library

Posted on 03/21/2025 By admin

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.

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

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

{
  "lastName": "Animesh",
  "firstName": "Prashant",
  "address": {
    "city": "Kolkata",
    "state": "WB"
  }
}
{
  "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.

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);
   }
}
[
  {
    "id": 1,
    "first_name": "Giavani",
    "last_name": "Skellorne"
  },
  {
    "id": 2,
    "first_name": "Patton",
    "last_name": "Buchett"
  }
]
[
  {
    "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.

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

Uncategorized

Post navigation

Previous Post: Selenium Framework 5: Understand Keyword Driven Framework in Selenium
Next Post: Log4j2 Tutorial 4 – Print Logs In External File Using XML Configuration File of Log4J2

Related Posts

Now You Can Minimize Browser in Selenium WebDriver Uncategorized
naviagte – Make Selenium Easy Uncategorized
Learn Selenium With Quiz – Advance Level 7 – XPath – Make Selenium Easy Uncategorized
Understand Verification & Validation In Software Testing Uncategorized
image – Make Selenium Easy Uncategorized
Postman tutorial Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark