Json Comaprision

Denis McMahon denismfmcmahon at gmail.com
Fri May 8 07:22:03 EDT 2015


On Tue, 05 May 2015 12:55:20 -0700, pra devOPS wrote:

> I wanted to compare two json files ignoring few of the keys in the json
> files.
> 
> Can anybody suggest me few things?

json files usually get imported to either a list or a dictionary (unless 
they're a simple string or number).

If the files are json arrays they'll get imported as lists. otherwise if 
they're key:value pairs they'll get imported as a dictionary.

Basically you need a routine that can take two arbitrary objects, check 
if they're the same data type, and then check their values. If they're of 
a collection type, checking their values means recursively checking that 
every element in the collection matches in type and value.

You need to be able to handle lists, dictionaries, strings, ordinals and 
floats, and for floats you might want to consider that if two floats are 
generated by two different systems, perhaps a cray xmp running compiled 
fortran and an acorn risc os box running compiled c++ for example, they 
might disagree at the least significant digit but still be considered 
equal for your purposes.

It's much easier to prove things are different than to prove they are the 
same, and you're much more likely to falsely detect difference than you 
are to correctly detect equality as the complexity of the objects you are 
checking increases unless you get the code right.

For a list, you need to consider if the order of elements is important. 
If not, then for every element in list a you need to detect if an 
identical element is in list b.

Supposing you have two lists of lists. This might mean that for every sub 
list in a, you need to check every sub list in b to see if it matches. 
You probably also want to check for lists and dictionaries if the sizes 
are equivalent too. Here's an example why:

a = [ [a,b,c] ]

b = [ [a,b,c], [a,c,b], [b,a,c], [b,c,a], [c,a,b], [c,b,a] ]

If you compare sublists purely on the basis that they contain the same 
elements, then a[0] == each of b[0..5]

Does that make a and b equal?

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list