Make Selenium Easy

REST Assured Tutorial 71 – Compare JSON Arrays Using JSONassert Library

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn to compare JSON arrays using the JSONassert library. We have already learned to compare JSON objects using the JSONassert library earlier.

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.

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

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 two JSON arrays in Lenient mode.

An equal number of elements, values and in the same order

Output

Since both JSON arrays have an equal number of elements and in same order, the comparison will pass.

An equal number of elements but different values

Output

It will try to find each and every element of the first JSON array in another array. It will not find “Amod” in the second array and will also report an unexpected element “Amod Mahajan” from the second array.

An equal number of elements but different case values

Output

By default, it will be case-sensitive.

An equal number of elements but different order

Output

Since we are using Lenient mode so the order of elements will not impact.

All unmatched values in an array

Let’s compare two JSON arrays whose elements are different. In this case, we will understand how actually check for equality is performed.

Output

It picks elements from the first array and tries to find that element in another array at any order as we are using Lenient mode. You can see in the above screenshot that it expected “Ravi” but none found and the same for other elements “Amod” and “Mukesh”. You can also see that it is showing unexpected elements i.e. “Mukesh”, “Swati”, and “Animesh” in the second array.

Different number of elements in arrays

We know that Lenient mode allows extensibility but not in the case of JSON arrays. Check the below example:-

Output

Strict mode

If we are concern about the ordering elements in an array then we can use strict mode. This is useful if we need to check if values are sorted or following a sequence based on some criteria.

Note the highlight in yellow. That is an index where the element did not match. See an example below:-

Note – If will give all unmatched elements. You must observe the same in other examples as well.

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

2 thoughts on “REST Assured Tutorial 71 – Compare JSON Arrays Using JSONassert Library

  1. how to compare JSON response with 100 records to XML response with 100 records
    say JSON response is like below (likewise it has 100 records) how to compare this response with XML response of 100 records, (for the same request)
    {
    “users”: [
    {
    “userId”: 1,
    “firstName”: “Krish”,
    “lastName”: “Lee”,
    “phoneNumber”: “123456”,
    “emailAddress”: “krish.lee@learningcontainer.com”
    },
    {
    “userId”: 2,
    “firstName”: “racks”,
    “lastName”: “jacson”,
    “phoneNumber”: “123456”,
    “emailAddress”: “racks.jacson@learningcontainer.com”
    },
    {
    “userId”: 3,
    “firstName”: “denial”,
    “lastName”: “roast”,
    “phoneNumber”: “33333333”,
    “emailAddress”: “denial.roast@learningcontainer.com”
    },
    {
    “userId”: 4,
    “firstName”: “devid”,
    “lastName”: “neo”,
    “phoneNumber”: “222222222”,
    “emailAddress”: “devid.neo@learningcontainer.com”
    },
    {
    “userId”: 5,
    “firstName”: “jone”,
    “lastName”: “mac”,
    “phoneNumber”: “111111111”,
    “emailAddress”: “jone.mac@learningcontainer.com”
    }
    ]
    }

    1. You need to compare those by writing logic. XML Path, JSON Path with Stream APIs of Java will help you to compare the output. I will plan to publish one post on that but that will differ based on response.

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 71 – Compare JSON Arrays Using JSONassert Library

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