I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:
{foo: 'bar', bar : 'baz'}
AFAIK, above is object literal, json as well as javascript object, isn't it?
Does object literal and json mean the same thing ?
How do you guys differentiate which is what?
I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:
{foo: 'bar', bar : 'baz'}
AFAIK, above is object literal, json as well as javascript object, isn't it?
Does object literal and json mean the same thing ?
How do you guys differentiate which is what?
Share Improve this question edited Jun 8, 2012 at 15:07 Sarfraz asked Jun 8, 2012 at 15:04 SarfrazSarfraz 383k82 gold badges559 silver badges612 bronze badges 6 | Show 1 more comment6 Answers
Reset to default 11The variable jsonString
contains a JSON string:
var jsonString = '{"foo": "bar", "bar" : "baz"}'
The variable javascriptObject
contains a javascript object, initialized using an object literal:
var javascriptObject = {foo: 'bar', bar : 'baz'}
You can convert a json string to a javascript object with JSON.parse
, and back again with JSON.stringify
.
JSON is a just a data format, like XML. True JSON should have the keys surrounded by double quotes, like so:
{"foo":"bar"}
JavaScript Objects are part of the JavaScript language, and have associated things such as a prototype.
Object literals is creating a javascript object in place with brackets as opposed to using the new
keyword, or Object.create()
.
//object literal
var foo = {};
//equivalent
var foo = new Object();
AFAIK, above is object literal, json as well as javascript object, isn't it?
It is an object literal. It creates an object.
It is not JSON, as it doesn't conform to the syntax (which is a subset of object literal notation). The keys are not quoted and the wrong kind of quote marks ('
instead of "
) are used around the values.
How do you guys differentiate which is what ?
Context.
JSON doesn't usually appear (embedded) in the middle of JavaScript programs. It is a data format and usually appears as whole files (or HTTP responses).
When something expects an object it could get one from an object literal or from a variable (or a return value from a function call, etc, etc).
JSON originates from the object literal notation of JavaScript and itself is a string. That explains the similarity, when just looking at it. Today JSON is used as a general means of serializing all kinds of data, before submitting it over some network or storing it.
// this is a JSON variable
var json = '{"foo": "bar", "bar" : "baz"}';
// obj is a JavaScript obj, defined by the object literal on the right hand side
var obj = {foo: 'bar', bar : 'baz'};
- JSON - serialized object; similar syntax as defining an object in JS
- Object literal - shorthand syntax to define an object in JS
- Object - the result of a definition by, e.g., an object literal
In JS you can convert a JSON string into an object by using
var obj = JSON.parse( json );
and get the JSON representation of an object (excluding attached functions) by
var json = JSON.stringify( obj );
According to the specification, in JSON all strings, whether they are values or keywords, should be surrounded by double quotes.
Your example would be a valid JSON string if it contains the following:
{"foo": "bar", "bar": "baz"}
Object Literal:
Referencing mozilla ,
An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).
Javascript Object:
Referencing mozilla,
In JavaScript, an object is a standalone entity, with properties and type
JSON:
Referencing mozilla and mozilla
JSON (JavaScript Object Notation) is a data-interchange format. It closely resembles a subset of JavaScript syntax, although it is not a strict subset.JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript.
In loose words,
An object
is a javascript variable which can have properties and type.
An object literal
is a way to assign properties and associated values to object
.
JSON is a more strict object literal
used for data interchange which is wrapped as string.
(Usually, strictness is there to allow all languages to use it, we cannot use function
as value, key
should always be in double quotes (In object literal its not compulsory))
JSON.stringify()
. Think of JSON as markup that is parsed into data structures. That markup may be sent to some non JavaScript environment, and parsed into whatever data structures make sense for that environment. – user1106925 Commented Jun 8, 2012 at 15:13foo=42;baz=21
. What do you think is this? One could think these are two JavaScript statements, assigning values to variables. But I my (made up) case, this is one line in a CSV file with;
as delimiter and has nothing whatsoever to do with JavaScript (or any other language). It's just a way to store and represent data. That's what JSON is. – Felix Kling Commented Jun 8, 2012 at 15:23{foo: 'bar', bar : 'baz'}
could be either an object literal or (invalid) JSON depending on where it's located. However, it is certainly not a JavaScript Object value - objects exist in memory, not in source code. – Šime Vidas Commented Jun 8, 2012 at 15:26