

When JSON is generated, arrays are often used where the data could be represented as objects. I want to compare two JSON strings in Java 8 for equality, but ignore specific known nodes which contain values which are expected to be different and are a. So it is arrays that cause most problems in comparing JSON data. Ī more intelligent ordered comparison might just say that 2 has been inserted. In this case, comparing with by position would give three differences: 2 != 4, 4 != 5 and 5 is a deleted item. So comparing by position or as unordered items are alternative approaches to be applied depending on the interpretation of the array data.įurthermore, comparing by position is not always what is needed when we use an array as a list, where the item order is significant. However, if the array is being used as an unordered set of numbers, then the arrays should be considered equal. For example, if an array is used to represent an x,y coordinate, then the expectation is that is not the same as . It compares each element according to their position in the. This is because arrays are used for different purposes. JSON diff tool makes a semantic comparison, it compares every attributevalue pairs of objects. If any one familiar with this, Could you please guide me the best approach. See the example below.Īrrays present more of a problem for comparison. JSON AĪny object that has a unique key member should ideally be represented as an object where the key is pulled out as the member string – this leads to unambiguous comparison.

Therefore corresponding members can be identified without ambiguity even if the order of the members is different. Objects also compare well in that each member property is identified by a string which should be unique within the object (it does not have to be unique but behaviour is unpredictable if they are not unique!).

Strings may also need some normalisation to handle special character encodings so that for example. Similarly 100 and 1e2 would also be deemed to be equal. The three literal names, true, falseand nullare not a problem, though note they must be lower case.īefore comparing two numbers, they should be normalised so that 1 and 1.0 would not show a change. Comparing two JSON files is fairly straightforward, though there are a few areas where it is not quite as simple as it seems.
