Many folks are confused about the difference between JSON and object literal because they have very similar syntax.

Object literal syntax is the simplest way to create javascript objects. You can easily create an object using object literal.

For instance:

var person = {
 firstName:"Brice"
 lastName :"Tienzale"
};

JSON, on the other hand, is widely used as language independent data transfer format between computer systems. It derives from Javascript object literal syntax, borrowing its simplicity, and therefore its syntax is very close to that of object literal. However, JSON format is language independent, so it is not a term only limited to javascript as opposed to object literal.

For example, the JSON data of person object above is :

{
 "firstName":"Brice"
 "lastName" :"Tienzale"
}

You can see the syntax of both are very similar. The only noticeable difference is that all names in JSON must be wrapped in double quotes.

If you create an object using JSON format, javascript engine treats it the same as you would have created the object using the object literal. Safe to say that all JSON data are valid Javascript object.

Javascript has built-in support for conversion between JSON and javascript object. To convert an object ‘obj’ to a JSON data, you can use JSON.stringify(obj). To create javascript object from JSON data ‘data1’, you can use JSON.parse(data1).

Reference:

https://medium.com/@easyexpresssoft/object-literal-vs-json-7a2084872907

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

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