Make Selenium Easy

REST Assured Tutorial 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn to compare the portion of JSON arrays and Objects using the JSONassert library.

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

Many times we need to compare specific portions of JSON Objects or JSON Arrays. For example – Suppose we create a new booking and get booking details. When we fetch all active bookings then we just need to verify booking details of newly booking in that active list.

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

Compare portions of JSON Objects

JSON Object 1

JSON Object 2

We need to compare that the “address” object in the first JSON Object has the same values for keys as “communicationAddress” in the second JSON Object. In this case, we just need to use the overloaded assertEquals() method with some methods with JsonPath to fetch the desired portion of JSON objects.

Example Program

Compare portions of JSON Arrays

JSON Array 1

JSON Array 2

In the above JSON Arrays, we just want to compare objects at the first index.

Example Program

You can also compare a portion of the JSON object to the portion of JSON Array. But remember you can not compare if both are of the not the same type. That will through compilation error.

ou 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 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library

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