Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 70 – Compare JSON Objects 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 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.

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 and different comparing modes.

Introduction To JsonAssert Library

JSONassert provides multiple static overloaded assertEquals() method. We will see most of them.

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.

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);
        }

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);
        }

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);
        }
FAILED: sameFieldsWithDifferentValues
java.lang.AssertionError: firstName
Expected: Amod
     got: Rahul

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);
        }
FAILED: unmatchedDataType
java.lang.AssertionError: age
Expected: 18
     got: 18

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);
        }
FAILED: strictMatchExample1
java.lang.AssertionError: 
Unexpected: age

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);
        }
}
FAILED: matchJsonObject
java.lang.AssertionError: Jsons are not equal firstName
Expected: Amod
     got: Rahul

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.");
                }
        }

}
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

Uncategorized

Post navigation

Previous Post: How To Change Default Download Directory For Chrome Browser in Selenium WebDriver
Next Post: Does getWindowHandles() method return handles in the same order as windows launch?

Related Posts

TestNG Tutorials 49: Need of DataProvider Method in TestNG Uncategorized
amod mahajan rest assured Uncategorized
August 2019 – Make Selenium Easy Uncategorized
April 23, 2017 – Make Selenium Easy Uncategorized
Postman Tutorial Part 51 -Printing Request Body & Response Body in Postman Console Uncategorized
image – Make Selenium Easy 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