REST Assured 71 - Compare JSON Arrays Using JSONassert Library

Keywords: Java JSON API testing

REST Assured series summary REST Assured 71 - Compare JSON Arrays Using JSONassert Library

introduce

API testing, sometimes you need to compare two JSON. For example, if we want to return the same result from an API every time, or part of the response body result is unchanged, we can directly compare the existing JSON response s, eliminating the need to write a lot of logic code to assert.

There are many good Java libraries that can be used for comparison, as we mentioned earlier Comparing two JSONs with Jackson Library In this article, we mainly know how to use the JSONassert library to compare two JSON response s.

prerequisite

add to JSONassert rely on

<!-- https://mvnrepository.com/artifact/org.skyscreamer/jsonassert -->
<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

Please read the following articles to understand the basic concepts of JSON assert Java library, different comparison modes and assertion JSON Objects.

Introduction To JsonAssert Library

Compare JSON Objects Using JSONassert Library

assertEquals() method

JSONassert provides many statically overloaded assertEquals() methods

Lenient mode

ahead article We have learned that in Lenient mode, extensibility is allowed without strict sequence requirements. We use examples to compare two Arrays in Lenient mode.

Compare two Arrays with the same elements, the same values and 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);

result:
Because the two array elements have the same number, values and order, the comparison result is PASS

Compare two Arrays with the same number of elements and different values

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

result:
Because there are different values, the result must FAIL

Compare two Arrays with the same number of elements and different value case

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

result:
The default is case sensitive, so the result is FAIL

Compare two Arrays with the same number of elements but different element order

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

result:
Because Lenient mode is used, the order of elements is not affected, so the result is PASS

Compare Arrays with two elements with different values

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

result:

As can be seen from the result output, first select elements from the first Array and try to find these elements in the second Array, not in order. "Ravi" is expected to be found in the second Array, but not found. Other elements "Amod" and "Mukesh" are the same. Unexpected elements "Mukesh", "Swati", and "Animesh" appear in the second Array.

Array comparing the number of two different elements
We know that Lenient mode allows extensibility, but it has no effect on Array type.

// Different no of elements
jsonArray1 = "[\"Amod\",\"Mukesh\",\"Ravi\"]";
jsonArray2 = "[\"Amod\",\"Mukesh\",\"Ravi\",\"Animesh\"]";
								
JSONAssert.assertEquals(jsonArray1, jsonArray2, JSONCompareMode.LENIENT);

result:

Strict mode

If we are concerned about the order of elements in an Array, we can use strict mode.

// 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 yellow highlight above to indicate which element does not match.

// 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);

Posted by smoked1 on Wed, 06 Oct 2021 17:55:14 -0700