Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Javascript Object and JSON object

Can anyone tell me the difference between Javascript Object and JSON object with an example?

like image 350
Refti Avatar asked Jun 27 '11 07:06

Refti


People also ask

Whats the difference between JSON and JavaScript?

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.

Is every JavaScript object JSON?

The answer is "no". There are cases when JSON object won't be valid for JavaScript. JSON is NOT a JavaScript subset.


2 Answers

A Javascript object is a data type in Javascript - it makes sense only in Javascript. Often you see a Javascript object literal like this:

var obj = {     a: 1,     b: 2 }; 

A JSON string is a data interchange format - it is nothing more than a bunch of characters formatted a particular way (in order for different programs to communicate with each other). Because of this, it can exist inside Javascript, or in another language or simply stored inside a database or a text file.

The above Javascript object can be represented in the JSON format in Javascript like this:

var json = '{ "a": 1, "b": 2 }'; 

Or in C# like this:

string json = "{ \"a\": 1, \"b\": 2 }"; 

As you can see, a JSON is simply stored inside a string. To make it useful, the JSON string can be parsed to produce an object in any language. Because the JSON format mimics Javascript's object literal syntax, Javascript makes the parsing process easy:

var obj = eval('(' + json + ')'); 

Though typically you'd see:

var obj = JSON.parse(json); // for security reasons 

Note that JSON is limited in that it cannot store functions - the only values it can contain are:

  • objects (literals)
  • arrays
  • numbers
  • booleans
  • strings
  • nulls
like image 97
David Tang Avatar answered Oct 02 '22 20:10

David Tang


JSON is a text representation of a javscript object. It is effectively an object literal in javascript notation (hence the name - JavaScript Object Notation => JSON).

If you want to "compare" two object, convert the text to objects then compare keys and values.

Some examples of objects to/from text:

// Create obj using an object literal var obj = {key: 'value'};  // Convert to text using JSON.stringify var text = JSON.stringify(obj);  // Show the value of text alert( text ); // {"key":"value"}  // Create a new object from text var newObj = JSON.parse(text); // javascript object  // Show the text version of newObj alert(JSON.stringify(newObj));  // {"key":"value"}  // Use text as code var newObj2 = eval('(' + text + ')');  // It is indeed a string literal alert(JSON.stringify(newObj2));  // {"key":"value"} 

If you want to compare two objects, convert them from JSON to objects (if they are JSON in the first place) then do something like:

function compareObjects(a, b) {   var i, p, aProps = [], bProps = [];    // Simple test first   if (a === b) {     return true;   }    // Get properties of a   for (p in a) {     if (a.hasOwnProperty(p)) {       aProps.push(p);     }    }    // Get properties of b   for (p in b ) {     if (b.hasOwnProperty(p)) {       bProps.push(p);     }    }    // If don't have same properties, return false   if (aProps.sort().join('') != bProps.sort().join('')) {     return false;   }    // If property values aren't the same, return false   i = aProps.length;   while (i--) {     if (a[aProps[i]] !== b[bProps[i]]) {       return false;     }   }    // If passed all tests, must be equal   return true; } 
like image 32
RobG Avatar answered Oct 02 '22 21:10

RobG