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

Introduction

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.

What happens when an ignored field is not present in simple JSON Object

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.

JSON Object 1

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

JSON Object 2

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

Sample Program

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

}
Output

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.

Solution

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

Example Program

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

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.

Leave a Reply

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