Make Selenium Easy

REST Assured Tutorial 69 – Introduction To JsonAssert Library


As a part of the End to End REST Assured Tutorial, in this post, we will learn about the JSONassert Java library which is used to assert two JSONs.

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 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 latest dependency of JSONassert. You can always get the latest dependency from Maven central repo.

Introduction to JSONassert

JSONassert Java library helps to assert JSON equality effectively. As per its GitHub document – Under the covers, JSONassert converts your string into a JSON object and compares the logical structure and data with the actual JSON. When strict is set to false (recommended), it forgives reordering data and extending results (as long as all the expected elements are there), making tests less brittle.

JSONassert class provides overloaded assertEquals() method to compare JSON objects and JSON arrays. We will learn more about the overloaded assertEquals() method in upcoming posts.

JSONassert provides an enum called JSONCompareMode which defines different modes that define different behavior for the comparison of JSON for testing. Each mode encapsulates two underlying behaviors: extensibility and strict ordering.

 ExtensibleStrict Ordering

These modes help a lot during comparing JSONs. For example – If we do not want to compare the whole JSONs instead we just concerned that some elements with value should be present within JSON. So we can use an extensible mode which will just check if the entire first JSON exists in the second JSON. The test will not fail if the second JSON consists of more elements/fields.

If extensibility not allowed, then all of the expected values must match in what’s being tested, but any additional fields will cause the test to fail. When extensibility is allowed, all values must still match.

JSON Object 1

JSON Object 2

If we select an extensible mode then

If we do not select an extensible mode then

We have another mode “strict” which controls assertion on the order of elements in JSON Arrays. Please note here I am mentioning the order for JSON arrays.

In the above JSONs we have a different order of fields but it will not fail with strict order mode as it is applied for JSON arrays not for JSON Objects.

If we have a JSON array as [1,2,3,5] and another JSON Array as [1,2,5,3] then strict order mode will fail the test as the order of elements in the array is not identical.

I hope I am able to explain basic concepts on JSONassert here as these concepts will be used in upcoming posts to show examples. Understanding extensible and strict mode is really important to use in our tests.

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. Currently getting paid in American Dollars. 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.