REST Assured Tutorial 70 – Compare JSON Objects using JSONassert Library


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.

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.

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.

Comparing two JSONs with the same fields but different values

In this scenario assertEquals() will fail as a value is not matching.


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.


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.


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.

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.

You can also print a custom message when JSONs are not identical.


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”.


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.

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.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.