Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in JSON objects using Javascript/JQuery

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

like image 689
Al. Avatar asked Jul 29 '09 14:07

Al.


People also ask

Are JSON objects the same as JavaScript objects?

JavaScript Objects VS JSON Though the syntax of JSON is similar to the JavaScript object, JSON is different from JavaScript objects. The key in key/value pair should be in double quotes. The key in key/value pair can be without double quotes. JSON cannot contain functions.

What is difference between JQuery and JSON?

Json: JSON is a text format that is completely language independent. JQuery:It is a fast and minified JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

How do you compare two JSON objects?

Comparing Json: Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

What are the differences between JSON and JavaScript syntax for declaring objects?

JSON is a data interchange format, which just happens to look like a subset of YAML or JavaScript code you can execute and get an object back. A JavaScript object is just an object in JavaScript. With JSON being a data interchange format you can exchange structured data in a textual form with it.


2 Answers

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.

like image 177
Peter Bailey Avatar answered Oct 17 '22 15:10

Peter Bailey


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.

like image 20
Benja Avatar answered Oct 17 '22 15:10

Benja