Make Selenium Easy

REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn to ignore specific nodes while comparing two JSON documents using the JSONassert library.

We have already learned to Compare two JSON Objects and Compare two JSON Arrays using JSONassert library as a whole.

We may not need to compare all nodes in JSON documents and need to ignore while comparing as a mismatch will fail the comparison. A real-time example might be a timestamp that may not be static and we must ignore those fields from comparison.

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.

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, different comparing modes, and asserting JSON Objects.

Introduction To JsonAssert Library

Compare JSON Objects Using JSONassert Library

Compare JSON Arrays Using JSONassert Library

How to ignore fields from JSON from comparison?

There is no direct way to ignore fields or attributes from a JSON document from comparison. We need to use CustomComparator and explicitly we need to make an assertion pass. A sample code for achieving the same is below:-

CustomComparator is a class and implements JSONComparator indirectly. Customization is another class. The constructor of CustomComparator takes JSONCompareMode and an array of types of Customization. If we see constructor of class Customization then you will find that it accepts a type of ValueMatcher which is an interface and contains a method equal().

In the above code snippet, I have used Lambda expressions and anonymous inner class concepts of Java to make code shorter otherwise you may need to write a lot of code.

If you are not aware of Lambda expressions in Java then you should learn them. I see many automation testing professionals avoid them.

To ignore fields from JSON for comparison using JSONassert is nothing but explicitly passing the assertion. If we get value v1 from the first JSON document and value v2 from the second JSON document and v1 may or may not equal to v2 then we need to explicitly mark them equal or something like a proxy.

salary” in the above code snippet is a JSON Path and “o1” and “o2” are values from JSON documents and without any condition, we are returning true. This is the way of ignoring fields from comparison using JSONassert. You may also have some manipulation or condition checks like substring or a format etc.

We can pass multiple Customization references together as I said CustomComparator takes an array of types of Customization.

We have overloaded assertEquals() method of JSONAssert class which takes JSONComparator reference.

Ignoring fields in a simple JSON object

JSON String 1

JSON String 2

I want to ignore the field “salary” from the comparison. Sample code is below-

Sample Code

Ignoring fields in a nested JSON object

JSON Object 1

JSON Object 2

I want to ignore the field “city” which is inside the “address” node from the comparison. We just need to provide a correct JSON path which will be “address.city“. Sample code is below-

Sample Code

Ignoring fields in a simple JSON array

JSON Array 1

JSON Array 2

Supposed I don’t want to compare “id” for the first indexed JSON Object. So we should give JSON path as “[0].id” but it WILL NOT ignore if we pass JSONCompareMode as LENIENT. For a specific index, we should pass STRICT.

Sample Code

If we want to ignore “id” for all indexed elements then we should pass the JSON path as “[*].id“. We can use STRICT or LENIENT mode in this case.

Ignoring fields in a nested JSON array

JSON Array 1

JSON Array 2

Suppose I would like to ignore the second mobile number of the first indexed element. We need to use JSON Path as “[0].mob[1].number“.

Sample Code

Note – There is a problem with the above approach. Tests will fail if any ignored element is not found in JSON objects to be compared. We will see the solution to that in the next 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

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

1 thought on “REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert

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 73 – How to ignore node/s for JSON comparison in JSONassert

by Amod Mahajan time to read: 7 min
1
%d bloggers like this: