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 jsonAlireza Masali– Alireza Masali2014-01-01 13:48:17 +00:00Commented Jan 1, 2014 at 13:48
-
1There's no such thing as a "JSON Object"Joseph Silber– Joseph Silber2014-01-01 13:49:59 +00:00Commented Jan 1, 2014 at 13:49
-
Possible Duplicate: stackoverflow.com/questions/2904131/…Sachin Jain– Sachin Jain2014-01-01 14:03:35 +00:00Commented Jan 1, 2014 at 14:03
-
Very useful information here cjihrig.com/blog/json-overviewSachin Jain– Sachin Jain2014-01-01 15:03:52 +00:00Commented Jan 1, 2014 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.
Comments
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.