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.



    org.skyscreamer
    jsonassert
    1.5.0
    test

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

// same no of elements, values and in same order
String jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
String jsonArray2 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
		
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);
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

// Same no of elements but different values
jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Amod Mahajan\",\"Mukesh\",\"Ravi\"]";
		
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);
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

// Same no of elements but different case values
jsonArray1 = "[\"AMOD\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
				
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);
Output

By default, it will be case-sensitive.

An equal number of elements but different order

// Same no of elements but different order
jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Mukesh\",\"Amod\",\"Ravi\"]";
 
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);

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.

// Same no of elements but all different values
jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Animesh\",\"Aaditya\",\"Swati\"]";
								
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);
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:-

// Different no of elements
jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Amod\",\"Mukesh\",\"Ravi\",\"Animesh\"]";
								
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);
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.

// same no of elements, values and in different order
String jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
String jsonArray2 = "[\"Mukesh\",\"Amod\",\"Ravi\"]";
		
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.STRICT);

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

// same no of elements, values and in different order
String jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
String jsonArray2 = "[\"Amod\",\"Anu\",\"Ravikant\"]";
		
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.STRICT);

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.

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

Your email address will not be published. Required fields are marked *