I have a unit test that compares two complex JSONObjects. I want to compare the whole of both of the objects but I want to ignore just one specific field (ie. timestamp. something which is always variable). Here is my code :
JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
JSONObject jsonObject1 = (JSONObject) parser.parse(expectedErrorMessage);
JSONObject jsonObject2 = (JSONObject) parser.parse(lastMessage.toString());
Assertions.assertEquals(jsonObject1, jsonObject2);
I was initially thinking the best way is to set a value in the JSONTree to null or some kind of arbitrary value. So the idea is some method that might look like this (like in javascript immutable library):
jsonObject = updateAJsonField(jsonObject, ["path","to", "field"], newValue)
Otherwise there is using a comparator in the equals, but it seems more trouble than its worth.
(ps i am using net.minidev.json)
Any ideas how to ignore a field deep in a complex JSON object?
Unfortunately I could not find anything on the internet that solved my problem, so I created my own Method which replaces a JSON value (using org.json.JSONObject) with a particular value.
Here is the test :