5

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?

Dev555
  • 2,128
  • 4
  • 30
  • 40
  • 1
    This question and its answers may be helpful: [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Drew Gaynor Jun 08 '12 at 15:12
  • 2
    It's very rare that you'll manually create a string in JavaScript to represent a JSON structure. Typically if there's any JSON data in your JavaScript, it will have been created using `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. –  Jun 08 '12 at 15:13
  • 2
    ...in other words, it's a standardized text format for transferring data between different programming environments. –  Jun 08 '12 at 15:18
  • Let me give you an other example: `foo=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 Jun 08 '12 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 Jun 08 '12 at 15:26
  • Thanks all it makes perfect sense now :) – Dev555 Jun 08 '12 at 15:27

6 Answers6

11

The 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.

Eric
  • 95,302
  • 53
  • 242
  • 374
7

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();
Ryan O'Donnell
  • 491
  • 2
  • 10
  • I like the answer, apart from the first sentence. Given just this line of code, it is neither JSON nor valid JavaScript (or object literal). For JSON, it misses the quotes around the keys and in JavaScript, since it is not in an expression context, the `{ }` would be interpreted as block delimiters (and not object literal indicators). So it's not all three, it is neither. – Felix Kling Jun 08 '12 at 15:28
4

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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

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 );
Sirko
  • 72,589
  • 19
  • 149
  • 183
2

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"}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • From the same link, "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested" – Jashwant Jun 08 '12 at 15:14
  • It would be valid JSON if it was `{"foo": "bar", "bar": "baz"}`... the surrounding quotes are invalid. – Felix Kling Jun 08 '12 at 15:14
  • @FelixKling I think by "valid JSON string" what they actually mean is "a string that contains valid JSON syntax". Or, to put it another way, a string that could be parsed as JSON to create an object. – Anthony Grist Jun 08 '12 at 15:17
  • 1
    @Anthony: Mmh... maybe. The term JSON *string* is quite ambiguous here. "String" could be meant as a data type (in JavaScript) (in which case the quotes are ok) or just as a sequence of characters (quotes would not be ok) (at least this is how I interpreted it as non-native English speaker). – Felix Kling Jun 08 '12 at 15:18
  • 1
    @Felix: to clarify (as neither am I a native English speaker): I meant a string as a sequence of characters *within* a string (datatype). – Marcel Korpel Jun 08 '12 at 15:32
0

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))

Jashwant
  • 28,410
  • 16
  • 70
  • 105