JavaScript Object VS JSON

JavaScript Object VS JSON

So what is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Then, what is JavaScript Object?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects.

Whattt??? So what’s the difference between them?

JSON is a data interchange format. It’s a standard which describes how ordered lists and unordered maps, strings booleans and numbers can be represented in a string. Just like XML and YAML is a way to pass structured information between languages, JSON is the same. A JavaScript object on the other hand is a physical type. Just like a PHP array, a C++ class/ struct, a JavaScript object is an type internal to JavaScript.

Let’s create a story. You went to IKEA and found a table you like. However, the only stock they have for the table is the display unit which is already assembled.

IKEA Table
IKEA Table == JavaScript Object

After you agreed to buy the display unit and paid the amount due, you need to disassemble the table into several smaller parts because it won’t fit into your car. This happens the same to the JavaScript object. Other languages cannot understand JavaScript object. In order to make them interchangeable, you have to convert it to interchangeable format so that the data can be understood by both participating programming languages. So you must turn the JavaScript object into a JSON. How? You can do this with JSON.stringify(). In other words, JavaScript object –stringify–> JSON.

Let’s JSON.stringify the object (ikeaTable):

Here is table in JSON format after it has been stringified:

 

Well the story doesn’t end there. Now that you have brought the IKEA table home, you need to assemble it before it can be used. Same goes to the JSON string. Now that you have receive the data in JSON format, and now you want to work with the data in JavaScript, you need to convert it back from JSON format to JavaScript object. How? You can do this with JSON.parse(). In other words, JSON –parse–> JavaScript object.

Let’s JSON.parse the JSON (table):

Here is tableObject in JavaScript object format after it has been parsed:

 

NOTE: JSON cannot represent functions or dates. If you attempt to stringify an object with a function member, the function will be omitted from the JSON representation. A date will be converted to a string. See example below:

NOTE 2: If you’re using jQuery, you can call jQuery.parseJSON(), which will use JSON.parse()under the hood if it’s supported and will otherwise fallback to a custom implementation to parse the input.

 

References:

Matt answer in Stack Overflow

Mozilla Developer Network

JSON.org

Leave a Reply

Your email address will not be published. Required fields are marked *