I'm learning javascript and just noticed that the syntax we use to define objects is the same as the json format. So I'm wondering if they are simply equivalent. Being more precise, does it mean that any javascript object (including its variables and functions) can be translated into json format and the same way around?
-
1no Objects are not Json format , you can convert your objecct properties to json . or a string of json to object. object has methods , constructors.... in javascript , u cant convert them to json – Alireza Masali Jan 01 '14 at 13:48
-
1[There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Joseph Silber Jan 01 '14 at 13:49
-
Possible Duplicate: http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation – Sachin Jain Jan 01 '14 at 14:03
-
Very useful information here http://cjihrig.com/blog/json-overview/ – Sachin Jain Jan 01 '14 at 15:03
3 Answers
JSON is (like it's name "JavaScript Object Notation" indicates) a subset* of the JavaScript syntax, i.e. (nearly) every* JSON is valid JavaScript but not the other way round.
Functions, for example, have no equivalent representation in JSON and therefore, cannot be translated into JSON. Since the main purpose for JSON is serialization, it doesn't provide representation of statements either.
* There is one exception: more or less all Unicode characters can be written literally in JSON but they must be written using escape sequences in JavaScript. See this blog post for more information. So not every valid JSON is valid JavaScript.

- 2,907
- 33
- 50
JSON data has only data specified in key-value pairs; where as js objects contain both data and functions enlisted in key-value pairs.
Think of JSON as a string representation of your javascript object. - String being the key word here.
Primarily used for easy transport over HTTP .It is a method created by Douglas Crockford to enable a friendly Javascript data transfer format similar to XML , RSS, et all.
JSON is generally parsed by server and client back into a Javascript object for use.

- 21,353
- 33
- 103
- 168

- 5,216
- 4
- 20
- 36