I have two JSON objects in Javascript, identical except for the numerical values. It looks like this:

var data = {
  "eth0":{"Tx":"4136675","Rx":"13232319"},
  "eth1":{"Tx":"4","Rx":"0"},
  "lo":{"Tx":"471290","Rx":"471290"}
}

var old = {
  "eth0":{"Tx":"4136575","Rx":"13232219"},
  "eth1":{"Tx":"4","Rx":"0"},
  "lo":{"Tx":"471290","Rx":"471290"}
}

One object called "data" has the current values, another object called "old" has the same values from 1 second ago. I'd like to output a JSON object with only the change in values so I can calculate data throughput on the network interfaces.

var throughput = {
  "eth0":{"Tx":"100","Rx":"100"},
  "eth1":{"Tx":"0","Rx":"0"},
  "lo":{"Tx":"0","Rx":"0"}
}

I'm not sure how to go about traversing the JSON data - it could be for any number of interfaces.

Can anyone please lend me a hand? Thanks in advance

share|improve this question

6 Answers

up vote 4 down vote accepted

You can iterate trough 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];
      }
    }
  }
}
share|improve this answer

This did the trick for me when dealing with a similar problem. It gets the differences in second compared to first.

    var first  = originalObj;
    var second = modifiedObj;
    var diff   = {};

    var differ = function(first, second, result) {
        var i = 0;
        for (i in first) {
            if (typeof first[i] == "object" && typeof second[i] == "object") {
                result[i] = differ(first[i], second[i], {});
                if (!result[i]) delete result[i];
            } else if (first[i] != second[i]) {
                result[i] = second[i];
            }
        }
        return isEmpty(result) ? undefined : result;
    }

    differ(old_conf, new_conf, diff);

Code is a bit of a special case, but you get the general idea :P

share|improve this answer

I wrote this page to visualize the difference. The source is not minified if you want to check it out. http://jsondiff.com/

share|improve this answer

Try http://prettydiff.com/ It is entirely written in JavaScript and works on both the client side and the server side. It is also algorithmic, so it can accurately diff between minified code and expanded code.

share|improve this answer

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.

share|improve this answer
+1 A sophisticated json diff algorithm, e.g. with string attributes, the decision whether to use the complete before and after string or do a diff inside the string, depending on the length of the resulting diff (I assume). – Eugene Beresovksy Aug 6 '12 at 10:28
Yes, a text diff is done when 2 long (how long is configurable) strings are found (in order to reduce diff size, and also to allow fuzzy patching). – Benja Aug 6 '12 at 14:57

did you look at : http://tlrobinson.net/projects/javascript-fun/jsondiff I think this is what you are looking for. :)

share|improve this answer
the project seems to be gone – Eugene Beresovksy Aug 6 '12 at 10:30

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.