views:

112

answers:

5

I am new to JSON and JavaScript object.

-Can anyone please explain the differences between JSON and JavaScript object?
-What are their uses?
-Is one better than the other? or it depends on the situation?
-When to use which one, in what situation?
-Why JSON was created in the first place? What was the main purpose?
-Can someone give examples of when one should use JSON rather JavaScript object and vice versa?

Thanks

A: 

http://en.wikipedia.org/wiki/JavaScript_Object_Notation

Happy reading! ^^

MatTheCat
+5  A: 

JSON is a string representation of an object. It is an interoperable serialization format. It is not tied only to javascript. For example there are JSON serializers for .NET allowing you to serialize/deserialize .NET objects.

So it's just a format allowing you to convert from objects to string and back which is convenient if you want to transfer them over the wire.

It is very close to javascript object representation and if you simply eval() a JSON string you will get the corresponding object.

Darin Dimitrov
Don't let Crockford hear you say that...
nickf
@nickf, the Crockford's json2.js library does just that, `eval` after some "regex validation", it doesn't do anything of *parsing* :P. In fact, even his *own library* has some deviations from [his own RFC](http://www.ietf.org/rfc/rfc4627.txt?number=4627)!, for example, json2.js can wrongly "parse" Octal literals, e.g.: `JSON.parse("01")`... I find it funny :P
CMS
@CMS Well I guess Doug would object to the phrase "simply `eval()`" then... (sans the regex validation and so on)
nickf
@Darin, so if i just write a program in JavaScript that needs to use object, i should not use JSON since I am not sending the object to other place?
Pheap
+12  A: 

First you should know what JSON is:

  • It is language agnostic data-interchange format.

The syntax of JSON was inspired by the JavaScript Object Literal notation, but there are differences between them.

For example, in JSON all keys must be quoted, while in object literals this is not necessary:

// JSON:
{ "foo": "bar" }

// Object literal:
var o = { foo: "bar" };

The quotes are mandatory on JSON because in JavaScript (more exactly in ECMAScript 3rd. Edition), the usage of reserved words as property names is disallowed, for example:

var o = { if: "foo" }; // SyntaxError in ES3

While, using a string literal as a property name (quoting the property name) gives no problems:

var o = { "if": "foo" }; 

So for "compatibility" (and easy eval'ing maybe?) the quotes are mandatory.

The data types in JSON are also restricted to the following values:

  • string
  • number
  • object
  • array
  • A literal as:
    • true
    • false
    • null

The grammar of Strings changes, the have be delimited by double quotes, while in JavaScript, you can use single or double quotes interchangeably.

// Invalid JSON:
{ "foo": 'bar' }

The accepted JSON grammar of Numbers also changes, in JavaScript you can use Hexadecimal Literals, for example 0xFF, or (the infamous) Octal Literals e.g. 010. In JSON you can use only Decimal Literals.

// Invalid JSON:
{ "foo": 0xFF }

There are some buggy implementations (Firefox 3.5+, IE8+, json2.js) where octal literals are wrongly allowed, e.g. JSON.parse('01') should produce a SyntaxError.

CMS
+1 Good answer but could do with emphasising the subsetting: any valid JSON declaration is also a valid JavaScript declaration, but not all valid JavaScript declarations are JSON declarations.
Daniel Earwicker
A: 

JSON is a data interchange format, which just happens to look like a subset of YAML or JavaScript code you can execute and get an object back. A JavaScript object is just an object in JavaScript.

With JSON being a data interchange format you can exchange structured data in a textual form with it. It is pretty decoupled from JavaScript by now. JavaScript object allow you to create and work with structured data during the execution of a JavaScript program.

Joey
A: 

@CMS describes it perfectly above, but i would say you can pretty much tell the difference by what JSON actually stands for... that is JavaScript Oject "Notation"!

Alex
-1 This should have been a comment on CMS's awnser
BGerrissen