In this tutorial, we’ll have a look at the JSONAssert library – a library focused on understanding JSON data.
Table of Contents
- Comparing two Exact same JSON Objects – LENIENT Mode
- Comparing two JSONs with the same fields but different in order – LENIENT Mode
- Comparing two JSONs with the same fields but different values – LENIENT Mode
- Comparing two JSONs with the same fields but different datatype of values – LENIENT Mode
- Comparing two JSONs by adding one more attribute to the second JSON – LENIENT Mode
- Comparing two JSONs with the addition – STRICT mode
- Comparing two JSONObjects – LENIENT Mode
Add the below-mentioned dependency in pom.xml.
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
There are two modes, strict and non-strict in JSONAssert. In most cases, you will probably want to set strict to false, since that will make the tests less brittle.
Strict tests require all the elements requested to be returned, and only those elements (ie, the tests are non-extensible). Arrays of elements must be returned to the same order as expected.
In Lenient mode extensibility will be allowed and no strict ordering will be checked. Let’s see example programs for comparing JSONs.
Comparing two Exact same JSON Objects – LENIENT Mode
import org.json.JSONObject;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
public class JsonAssertDemo {
@Test
public void exactSameJson() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
// Lenient mode - extensible and no strict ordering
JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-23.png?w=703)
Comparing two JSONs with the same fields but different in order – LENIENT Mode
@Test
public void sameJsonWithDifferentOrder() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 = "{ " +
"\"last_name\": \"Singh\"," +
"\"first_name\" : \"Vibha\"" +
"}";
JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-24.png?w=743)
LENIENT as well as STRICT mode does not care about the order of the fields in the JSON.
Comparing two JSONs with the same fields but different values – LENIENT Mode
@Test
public void sameJsonWithDifferentValues() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Verma\"" +
"}";
JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-25.png?w=1030)
In this scenario assertEquals() will fail as a value is not matching.
Comparing two JSONs with the same fields but different datatype of values – LENIENT Mode
@Test
public void sameJsonWithDifferentDataType() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"," +
"\"salary\": 115000" +
"}";
String jsonObject2 ="{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"," +
"\"salary\": \"115000\"" +
"}";
JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-26.png?w=1045)
Here, 115000 is an integer in the first string and a string in the second one. These are not the same. So, the test fails.
Comparing two JSONs by adding one more attribute to the second JSON – LENIENT Mode
@Test
public void differentJson() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 ="{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"," +
"\"salary\": \"115000\"" +
"}";
JSONAssert.assertEquals(jsonObject1, jsonObject2, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-27.png?w=693)
Here, both the JSONs have the same structure, but we have added one more attribute to the second one. In this case, the assertion matches the first string with the second one and it looks the same. So, the test passes.
Let us reverse the assertion now. We are comparing the jsonObject2 which has 3 attributes with jsonObject1 which has 2 attributes. So, salary is not present in jsonObject1. In this case, the test fails.
@Test
public void differentJson() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 ="{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"," +
"\"salary\": \"115000\"" +
"}";
JSONAssert.assertEquals(jsonObject2, jsonObject1, JSONCompareMode.LENIENT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-28.png?w=1016)
Comparing two JSONs with the addition – STRICT mode
@Test
public void differentJsonWithStrict() {
String jsonObject1 = "{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"" +
"}";
String jsonObject2 ="{ " +
"\"first_name\" : \"Vibha\"," +
"\"last_name\": \"Singh\"," +
"\"salary\": \"115000\"" +
"}";
JSONAssert.assertEquals("JSONs are not equal",jsonObject1, jsonObject2, JSONCompareMode.STRICT);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-29.png?w=786)
The compare mode can also be defined by using an overloaded method that takes boolean instead of JSONCompareMode where LENIENT = false and STRICT = true.
JSONAssert.assertEquals(jsonObject2, jsonObject1, JSONCompareMode.LENIENT);
JSONAssert.assertEquals(jsonObject2, jsonObject1, false);
JSONAssert.assertEquals(jsonObject2, jsonObject1, JSONCompareMode.STRICT);
JSONAssert.assertEquals(jsonObject2, jsonObject1, true);
Comparing two JSONObjects – LENIENT Mode
@Test
public void matchJsonObject() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("first_name", "Vibha");
jsonObject1.put("last_name", "Singh");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("first_name", "Vibha");
jsonObject2.put("last_name", "Verma");
JSONAssert.assertEquals("JSONs are not equal", jsonObject1, jsonObject2, false);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/12/image-30.png?w=838)
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!