Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
JSON Diff Released (ajaxian.com)
19 points by ajbatac on June 30, 2008 | hide | past | favorite | 6 comments


Interesting. Unfortuantely JSON can't distinguish between lists and sets. So you can't get a good diff of arrays.

Does:

    ['foo','bar','baz'] -> ['baz','bar','foo']
Diff to:

   @@ -1,3 +1,3 @@
   -baz
   +foo
    bar
   -foo
   +baz
Or are they the same?


You can implement a set as an object, though. One presumes (not having tried the tool) that it's not sensitive to order differences in object field initializers.

Basically, "set" isn't a native data structure in Javascript, so it doesn't exist in JSON either. You can use arrays for that purpose, but just as with code, you need to be a little careful. Probably you should consider storing all your "sets" in a normalized (e.g. sorted) form.


JavaScript (and thus JSON) arrays are lists, not sets. You can implement sets using arrays, but this diffs JSON, not your implementation of sets.

It diffs arrays "correctly", though not intelligently. If you have [1,2,3,4,5] and [1,3,4,5] it won't tell you the item at index 1 was removed, it will tell you the items at index 1, 2, 3 were changed and index 4 was removed. Thus it does need a little work, but not in the context you described.

p.s. I'm the author of the JSON Diff tool.


Why don't you try it, rather than post a comment with misleading statements like "you can't get a good diff of arrays"?


It's easy enough to check, and they do in fact diff to the above.


Sure, but that's decided by the diff tool, not by the data.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: