Difference in JSON objects using Javascript/JQuery

Solution 1:

The basic premise for iterating over objects in JavaScript is like so

var whatever = {}; // object to iterate over
for ( var i in whatever )
{
  if ( whatever.hasOwnProperty( i ) )
  {
     // i is the property/key name
     // whatever[i] is the value at that property
  }
}

Fixing up a checker wouldn't be too hard. You'll need recursion. I'll leave that as an exercise for you or another SOer.

Solution 2:

Maybe it's already answered enough, but let me add my shameless plug :) A JSON (actually any javascript object or array structure) diff & patch library I open sourced at github:

https://github.com/benjamine/jsondiffpatch

it generates diffs (also in JSON format, and with a small footprint), which you can use client (check the test page) & server side, and if present, it uses http://code.google.com/p/google-diff-match-patch/ for long strings automatically.

check the DEMO page to see how it works.

Solution 3:

You can iterate through the parent and child object properties:

var diff = {};
for(var p in data){
  if (old.hasOwnProperty(p) && typeof(data[p]) == 'object'){
    diff[p] = {};
    for(var i in data[p]){
      if (old[p].hasOwnProperty(i)){
        diff[p][i] = data[p][i] - old[p][i];
      }
    }
  }
}