Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 69 – Introduction To JsonAssert Library

Posted on 02/19/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn about the JSONassert Java library which is used to assert two JSONs.

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 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.

I have used available latest dependency of JSONassert. You can always get the latest dependency from Maven central repo.

  org.skyscreamer jsonassert 1.5.0 test

JSONassert Java library helps to assert JSON equality effectively. As per its GitHub document – Under the covers, JSONassert converts your string into a JSON object and compares the logical structure and data with the actual JSON. When strict is set to false (recommended), it forgives reordering data and extending results (as long as all the expected elements are there), making tests less brittle.

JSONassert class provides overloaded assertEquals() method to compare JSON objects and JSON arrays. We will learn more about the overloaded assertEquals() method in upcoming posts.

JSONassert provides an enum called JSONCompareMode which defines different modes that define different behavior for the comparison of JSON for testing. Each mode encapsulates two underlying behaviors: extensibility and strict ordering.

  Extensible Strict Ordering
STRICT no yes
LENIENT yes no
NON_EXTENSIBLE no no
STRICT_ORDER yes yes

These modes help a lot during comparing JSONs. For example – If we do not want to compare the whole JSONs instead we just concerned that some elements with value should be present within JSON. So we can use an extensible mode which will just check if the entire first JSON exists in the second JSON. The test will not fail if the second JSON consists of more elements/fields.

If extensibility not allowed, then all of the expected values must match in what’s being tested, but any additional fields will cause the test to fail. When extensibility is allowed, all values must still match.

JSON Object 1

{
  "firstName" : "Amod",
  "lastName": "Mahajan",
  "age": "28"
}

JSON Object 2

{
  "firstName" : "Amod",
  "lastName": "Mahajan",
  "age": "28",
  "address" : "Bengaluru"
}

If we select an extensible mode then

JsonObject 1 == JsonObject2  => true
JsonObject 2 == JsonObject1  => false ("address" field is missing in 2nd json)

If we do not select an extensible mode then

JsonObject 1 == JsonObject2  => false ("address" field is extra in 2nd JSON)
JsonObject 2 == JsonObject1  => false ("address" field is missing in 2nd json) 

We have another mode “strict” which controls assertion on the order of elements in JSON Arrays. Please note here I am mentioning the order for JSON arrays.

{
  "firstName" : "Amod",
  "lastName": "Mahajan"
}
{
  "lastName": "Mahajan",
  "firstName" : "Amod"
}

In the above JSONs we have a different order of fields but it will not fail with strict order mode as it is applied for JSON arrays not for JSON Objects.

If we have a JSON array as [1,2,3,5] and another JSON Array as [1,2,5,3] then strict order mode will fail the test as the order of elements in the array is not identical.

I hope I am able to explain basic concepts on JSONassert here as these concepts will be used in upcoming posts to show examples. Understanding extensible and strict mode is really important to use in our tests.

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

Uncategorized

Post navigation

Previous Post: Javafaker API
Next Post: FREQUENTLY ASKED JAVA PROGRAMS 35 – Java Program to Reverse a String Using Stack

Related Posts

February 13, 2019 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
April 30, 2018 – Make Selenium Easy Uncategorized
August 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
jsonassert tutorials Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark