Presenting recursive dict (json_diff)

mcepl mcepl at redhat.com
Thu Oct 27 04:50:04 EDT 2011


Hi,

I have here a simple script (https://gitorious.org/json_diff/mainline)
which makes a diff between two JSON files. So for JSON objects

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

and

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

it generates

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

(obvious problems with name conflicts between internal keys and the
investigated keys will be somehow addressed later; any simple Pythonic
suggestions how?)

Now, I would like to create a script (or class) to present such diff
object in HTML (and mind you the diffs might be large, hopefully not
too much deeply nested, by with many many keys).

Any suggestions how to do it? Any libraries to use? I would love to
use some templating language, but I am not sure which simple ones
(e.g., I like pystache) could do recursive templating. So currently I
am tending towards generating strings.

I am currently tending towards something like (not cleaned to be CSS-
only yet):

    <table>
      <tr>
        <td> </td>
        <td class="append_class">'c' = 3</td>
      </tr>
      <tr>
        <td class="remove_class">'b' = 2</td>
        <td>--</td>
      </tr>
      <tr>
        <td class="update_class" colspan="2">'a' = 2</td>
      </tr>
      <tr>
        <td class="update_class" colspan="2">children</td>
      </tr>
      <tr>
        <td>*</td>
        <td class="update_class" colspan="2">son = 'Ivánek'</td>
      </tr>
      <tr>
        <td>*</td>
        <td class="append_class" colspan="2">daughter =
        'Maruška'</td>
      </tr>
    </table>

but I cannot say I like it. Any suggestions?

Thank,

Matěj



More information about the Python-list mailing list