Felix Kling

Javascript Json Objects And Object Literals

JavaScript, JSON objects and object literals #

This is my crusade against the inconsiderate usage of the term JSON object in the context of JavaScript.

JSON and JSON objects #

JSON objects are defined as follows:

JSON object

Where value can be:

So this is a valid JSON string with a JSON object:

{"foo": "bar", "baz": 0}

Object literals in JavaScript #

Some people think, the object literals in JavaScript and JSON objects are the same and use the latter to describe the former. But that is wrong. The syntax for JSON objects is just a subset of the possible syntax for JavaScript objects. The object literal notation was just the model for JSON.

This is not a JSON object:

var foo =  {foo: "bar", baz: 0};

It is an object literal. It would be only valid to talk about a JSON object in this case:

var foo = '{"foo": "bar", "baz": 0}';

because now here we have a string that contains JSON and a JSON object.

The JSON object in JavaScript #

Using the term JSON object for object literal notation seems to be even more wrong considering that there actually exists a JSON object in JavaScript. At least in the newer browsers. It is an object, available via the global variable JSON and provides the methods

Conclusion #

Don’t use the term JSON object to describe JavaScript objects defined with object literal notation. It is misleading and confusing.


Further information: #