Make Selenium Easy

REST Assured Tutorial 74 – Problem With JSONassert While Ignoring Fields From JSON Documents

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn about some challenges and its solution with JSON assert.

I have covered concepts of JSONassert library which are used to compare JSON documents. But comparing JSON documents as a whole with some customization like ignoring some fields is not so simple and proper when ignoring field is not present in JSON documents to be compared.

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.

What happens when an ignored field is not present in simple JSON Object

We know how to ignore fields from a comparison in JSON Objects but we have a scenario where the ignored field may not be present in JSON objects. In the case of dynamic JSON Objects, we may not know if the field will be present always. Let’s run an example program and observe the output.

Please note problem occurs when a field is not present in the second JSON object. If a field is missing from the first JSON Object there will be no error.

JSON Object 1

JSON Object 2

As we can see “Salary” is missing from the second JSON Object. We want to ignore this field from comparison as we assume that this field may or may not present. For a scenario that may be salary of board members will not be visible.

Sample Program

Output

From the above example, we can learn that if an ignored field is not present in the second JSON object then we will get an assertion error as above. This is not what we wanted. If a field is ignored then it should be ignored without checking if it is missing from the second JSON Object. But it does not work in this way.

There is no direct solution to it. Instead, we need to override multiple methods.

Solution

If you debug the code while comparing JSON using JSONassert then you will find a method named “checkJsonObjectKeysExpectedInActual()” in an abstract class “AbstractComparator” which is called internally. The default implementation is as below:-

By default, they check for the presence of a key in JSON Object and fail if is not missing. We can override this method and can remove ignored keys directly.

Example Program

This time comparison will pass which was failing above but this will not work if nested JSON object or arrays. You may need to override many methods to handle these scenarios.

You can also use the above concept with asserting portion of JSON document as discussed in this post.

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

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.