Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

REST Assured Tutorial 74 – Problem With JSONassert While Ignoring Fields From JSON Documents

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 some challenges and its solution with JSON assert.

I have covered concepts of JSONassert library which are used to compare JSON documents. But comparing JSON documents as a whole with some customization like ignoring some fields is not so simple and proper when ignoring field is not present in JSON documents to be compared.

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.

We know how to ignore fields from a comparison in JSON Objects but we have a scenario where the ignored field may not be present in JSON objects. In the case of dynamic JSON Objects, we may not know if the field will be present always. Let’s run an example program and observe the output.

Please note problem occurs when a field is not present in the second JSON object. If a field is missing from the first JSON Object there will be no error.

{
  "id": 1,
  "first_name": "Amod",
  "last_name": "Mahajan",
  "married": false,
  "salary": 123.45
}
{
  "id": 1,
  "first_name": "Amod",
  "last_name": "Mahajan",
  "married": false
}

As we can see “Salary” is missing from the second JSON Object. We want to ignore this field from comparison as we assume that this field may or may not present. For a scenario that may be salary of board members will not be visible.

package IgnoringFieldsDuringComparision;

import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;

public class IgnoringFieldsFromSimpleJsonObjectsNonExistence {
        
        public static void main(String[] args) throws JSONException {
                
                String s1 = "{\r\n" + 
                                "  \"id\": 1,\r\n" + 
                                "  \"first_name\": \"Amod\",\r\n" + 
                                "  \"last_name\": \"Mahajan\",\r\n" + 
                                "  \"married\": false,\r\n" + 
                                "  \"salary\": 123.45\r\n" + 
                                "}";
                
                String s2 = "{\r\n" + 
                                "  \"id\": 1,\r\n" + 
                                "  \"first_name\": \"Amod\",\r\n" + 
                                "  \"last_name\": \"Mahajan\",\r\n" + 
                                "  \"married\": false\r\n" + 
                                "}";
                
                JSONComparator com = new CustomComparator(JSONCompareMode.LENIENT, 
                                new Customization("salary", (o1, o2) -> true));
                
                JSONAssert.assertEquals(s1, s2, com);
                
        }

}

From the above example, we can learn that if an ignored field is not present in the second JSON object then we will get an assertion error as above. This is not what we wanted. If a field is ignored then it should be ignored without checking if it is missing from the second JSON Object. But it does not work in this way.

There is no direct solution to it. Instead, we need to override multiple methods.

If you debug the code while comparing JSON using JSONassert then you will find a method named “checkJsonObjectKeysExpectedInActual()” in an abstract class “AbstractComparator” which is called internally. The default implementation is as below:-

protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException { Set expectedKeys = getKeys(expected); for (String key : expectedKeys) { Object expectedValue = expected.get(key); if (actual.has(key)) { Object actualValue = actual.get(key); compareValues(qualify(prefix, key), expectedValue, actualValue, result); } else { result.missing(prefix, key); } } }

By default, they check for the presence of a key in JSON Object and fail if is not missing. We can override this method and can remove ignored keys directly.

package JsonObject; import java.util.Set; import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.CustomComparator; public class MyComparator extends CustomComparator{ private final Set attributesToIgnore; public MyComparator(JSONCompareMode mode, Set attributesToIgnore, Customization... customizations) { super(mode, customizations); this.attributesToIgnore = attributesToIgnore; } @Override protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException { //Remove ignored keys from json object attributesToIgnore.forEach(attribute -> expected.remove(attribute)); super.checkJsonObjectKeysExpectedInActual(prefix, expected, actual, result); }
}
package IgnoringFieldsDuringComparision; import java.util.Arrays;
import java.util.HashSet; import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.JSONComparator; public class IgnoreFieldsUsingMyComparator { public static void main(String[] args) throws JSONException { String s1 = "{\r\n" + " \"id\": 1,\r\n" + " \"first_name\": \"Amod\",\r\n" + " \"last_name\": \"Mahajan\",\r\n" + " \"married\": false,\r\n" + " \"salary\": 123.45\r\n" + "}"; String s2 = "{\r\n" + " \"id\": 1,\r\n" + " \"first_name\": \"Amod\",\r\n" + " \"last_name\": \"Mahajan\",\r\n" + " \"married\": false\r\n" + "}"; JSONComparator com = new MyComparator(JSONCompareMode.LENIENT, // Pass attributes to ignore new HashSet(Arrays.asList("salary")), new Customization("firstName", (o1, o2) -> true), new Customization("age", (o1, o2) -> true)); JSONAssert.assertEquals(s1, s2,com); } }

This time comparison will pass which was failing above but this will not work if nested JSON object or arrays. You may need to override many methods to handle these scenarios.

You can also use the above concept with asserting portion of JSON document as discussed in this post.

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

Uncategorized

Post navigation

Previous Post: Cucumber Tutorial 2 – Setup a Basic Maven-Cucumber 5 -TestNG Project in Eclipse
Next Post: Are You Getting NullPointerException For Second TestNG Test While Running As A Suite?

Related Posts

noSuchElementException Uncategorized
Using Implicit Wait in Selenium WebDriver Uncategorized
Java Programs | Make Selenium Easy Uncategorized
May 27, 2019 – Make Selenium Easy Uncategorized
Git Tutorial 2 – What is GIT and Its Advantages? Uncategorized
JSONPath and Query JSON using JSONPath Uncategorized

Recent Posts

  • How to Set Up Selenium WebDriver with Python Step by Step
  • How to Set Up Selenium WebDriver with Java from Scratch
  • What Is Selenium and Why It Is the Most Popular Test Automation Framework
  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing

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