Converting such an array into an object may therefore be a sensible pre-comparison step in order to get only ‘real’ changes identified. This tool allows to compare two JSON data structures, and visualize the diff. arrays) sort) as b a b' This program should return 'true' or 'false' depending on whether or not the objects are equal using the definition of equality you ask for. One of the arrays (source) is based on the data in an Excelsheet. Assuming your two files are named a.json and b.json, on the latest jq nightly: jq -argfile a a.json -argfile b b.json -n ' (a (. The arrays are created with in the flow based on source data, the arrays use the same schema, so the object within the array are comparable. When JSON is generated, arrays are often used where the data could be represented as objects. I am trying to build logic in flow to compare two JSON arrays. 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. If we have one array in one order: 10, 20, 30 assertTrue (parser.parse (string1). 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. Compare Two JSON Arrays In a case of JSON arrays, JsonParser will return a JsonArray. 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 . We will cover two of them: JSON.stringify() Comparing with JSON.stringify() may not work for more. Arrays and objects are checked for deep equality. This is because arrays are used for different purposes. There are several ways to compare two arrays in JavaScript. The equality operator returns Boolean true if both operands are the same (type and value). Returns a dynamic (JSON) array of the set of all distinct values that are in the first array but arent in other arrays - ( ( (arr1 arr2) arr3). 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.
0 Comments
Leave a Reply. |