Javascript: Object Literal vs. JSON

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:

object literal

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

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)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store