![2 json compare 2 json compare](https://filiphric.com/_nuxt/img/devtools.f70c981.png)
![2 json compare 2 json compare](https://i.ytimg.com/vi/88QqeFKHHo8/maxresdefault.jpg)
JObject xpctJSON JObject.Parse (expectedJSON) JObject actJSON JObject.Parse (actualJSON) bool res JToken.DeepEquals (xpctJSON, actJSON) But can't find anything that returns the diff's. When JSON is generated, arrays are often used where the data could be represented as objects. I'd like to get a list of the JSON parts that don't match when doing a comparison using Newtonsoft. 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.
![2 json compare 2 json compare](https://i.stack.imgur.com/dqI6k.png)
For example, if an array is used to represent an x,y coordinate, then the expectation is that is not the same as . This is because arrays are used for different purposes. 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. Converting such an array into an object may therefore be a sensible pre-comparison step in order to get only ‘real’ changes identified.Comparing two JSON files is fairly straightforward, though there are a few areas where it is not quite as simple as it seems. When JSON is generated, arrays are often used where the data could be represented as objects. Any 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.