json_diff ... structured diff for JSON files

Po 24 října 2011

Just to let the world know that I have just created json_diff.py, which is very simple Python script to generate a JSON of differences between two JSON files. One would thing that such thing must exist already, but all what I was able to found was not working much (http://tlrobinson.net/projects/javascript-fun/jsondiff/ is kind of weird, and spits out HTML directly) or at all (I won't name them), or was incredibly complex (XDiff, http://pages.cs.wisc.edu/~yuanwang/xdiff.html which is super machine for comparing XML files, but 3+k lines of (mostly) uncommented Java).

So, my script generates from these two JSON files:

{
    "a": 1,
    "b": 2,
    "son": {
        "name": "Janošek"
    }
}

and:

{
    "a": 2,
    "c": 3,
    "daughter": {
        "name": "Maruška"
    }
}

this result:

{
    "append": {
        "c": 3,
        "daughter": {
            "name": "Maruška"
        }
    },
    "remove": {
        "b": 2,
        "son": {
            "name": "Janošek"
        }
    },
    "update": {
        "a": 2
    }
}

(yes, it should work correctly with Unicode strings). If anybody has any comments on the script or (especially) on the output format, let me know. Of course, patches welcome (I have forgotten to state the license, but let's say MIT/X11 and if you want anything else, let me know as well).

Also, if anybody wants to get into super boring debugging/refactoring task, then the branch rewrite_xdiff has syntactically correct, but non-working port of XDiff Java program. See the original XDiff paper to see what beauty is (well) hidden there.

Category: computer Tagged: python json

Page 1 of 1