Make Selenium Easy

REST Assured Tutorial 70 – Compare JSON Objects using JSONassert Library

Introduction

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.

Output

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.

Output

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.

Output

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.

Output

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

Output

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

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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

REST Assured Tutorial 70 – Compare JSON Objects using JSONassert Library

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: