kayduh
kayduh

Reputation: 318

Comparing python dictionaries and find diffrence of the two

So im trying to write a python program that will take 2 .json files compare the contents and display the differences between the two. So far my program takes user input to select two files and compares the two just fine. I have hit a wall trying to figure out how to print what the actual differences are between the two files.

my program:

#!/usr/bin/env python2

import json

#get_json() requests user input to select a .json file
#and creates a python dict with the information
def get_json():
    file_name = raw_input("Enter name of JSON File: ")
    with open(file_name) as json_file:
        json_data = json.load(json_file)
        return json_data

#compare_json(x,y) takes 2 dicts, and compairs the contents
#print match if equal, or not a match if there is difrences
def compare_json(x,y):
    for x_values, y_values in zip(x.iteritems(), y.iteritems()):
        if x_values == y_values:
            print 'Match'
        else:
            print 'Not a match'

def main():
    json1 = get_json()
    json2 = get_json()
    compare_json(json1, json2)

if __name__ == "__main__":
    main()

example of my .json:

{
    "menu": {
        "popup": {
            "menuitem": [
                {
                    "onclick": "CreateNewDoc()",
                    "value": "New"
                },
                {
                    "onclick": "OpenDoc()",
                    "value": "Open"
                },
                {
                    "onclick": "CloseDoc()",
                    "value": "Close"
                }
            ]
        },
        "id": "file",
        "value": "File"
    }
}

Upvotes: 0

Views: 6713

Answers (2)

Nilav Baran Ghosh
Nilav Baran Ghosh

Reputation: 1349

You might want to try out the jsondiff library in python. https://pypi.python.org/pypi/jsondiff/0.1.0

The examples referenced from the site are below.

>>> from jsondiff import diff

>>> diff({'a': 1}, {'a': 1, 'b': 2})
{<insert>: {'b': 2}}

>>> diff({'a': 1, 'b': 3}, {'a': 1, 'b': 2})
{<update>: {'b': 2}}

>>> diff({'a': 1, 'b': 3}, {'a': 1})
{<delete>: ['b']}

>>> diff(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])
{<insert>: [(3, 'd')]}

>>> diff(['a', 'b', 'c'], ['a', 'c'])
{<delete>: [1]}

# Similar items get patched
>>> diff(['a', {'x': 3}, 'c'], ['a', {'x': 3, 'y': 4}, 'c'])
{<update>: [(1, {<insert>: {'y': 4}})]}

# Special handling of sets
>>> diff({'a', 'b', 'c'}, {'a', 'c', 'd'})
{<add>: set(['d']), <discard>: set(['b'])}

# Parse and dump JSON
>>> print diff('["a", "b", "c"]', '["a", "c", "d"]', parse=True, dump=True, indent=2)
{
  "$delete": [
    1
  ],
  "$insert": [
    [
      2,
      "d"
    ]
  ]
}

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 113945

Your problem stems from the fact that dictionaries are stored in a structure with an internal logical consistency - when you ask for someDict.items() and someOtherDict.items(), the key-value pairs of elements are computed by the same algorithm. However, due to differences in the keys that may be present in either dictionary, identical keys may not be present in the corresponding index in either list returned by the call to dict.items(). As a result, you are much better off checking if a particular key exists in another dictionary, and comparing the associated value in both.

def compare_json(x,y):
    for x_key in x:
        if x_key in y and x[x_key] == y[x_key]:
            print 'Match'
        else:
            print 'Not a match'
    if any(k not in x for k in y):
        print 'Not a match'

If you want to print out the actual differences:

def printDiffs(x,y):
    diff = False
    for x_key in x:
        if x_key not in y:
            diff = True
            print "key %s in x, but not in y" %x_key
        elif x[x_key] != y[x_key]:
            diff = True
            print "key %s in x and in y, but values differ (%s in x and %s in y)" %(x_key, x[x_key], y[x_key])
    if not diff:
        print "both files are identical"

Upvotes: 3

Related Questions