Converting such an array into an object may therefore be a sensible pre-comparison step in order to get only ‘real’ changes identified. When JSON is generated, arrays are often used where the data could be represented as objects. 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 . 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. However this seems like it is written for equality checks, and doesn't provide that much visual feedback.Comparing two JSON files is fairly straightforward, though there are a few areas where it is not quite as simple as it seems. PPS! Here is a tool in python to compare XML (which possibly could be changed into comparing json (if converted into an etree)). PS! On a side-note it does handle html, so possibly it can handle xml also? No data is transfered to a central server, the comparison is performed entirely in the. In order to make the comparison meaningful, it formats the YAML, including sorting the keys, to then performs a texual comparison. I'm not sure if it actually suggests that code is moved, or if that requires some manual labour, but it does claim in the functions view to be able to detect if it is modified, removed or added. YAML Diff compares YAML, and because YAML is a a superset of JSON, it can compare JSON as well. The program is not freeware (USD 29.95/user), but you can try it without a license for 30 days.
0 Comments
Leave a Reply. |