1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
json_diff ... structured diff for JSON files
############################################
:date: 2011-10-24T23:16:59
:category: computer
:tags: python, json
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.
.. _`XDiff paper`:
http://pages.cs.wisc.edu/~yuanwang/papers/xdiff.pdf
|