There's no such thing as a "JSON object" By"Cowboy" Ben AlmanOn March 3, 2010 pm | 15 comments and 0 Reactions
I want to clear up a common misconception. it's my belief that developers mistakenly call JavaScript Object literals "JSON objects" because their syntax is identical to (well, a superset of) what is described in the JSON specification, but what the specification fails to mention explicitly is that since JSON is a "data-interchange language ,"It's only actually JSON when it's used in a string context.
Serialization and deserialization
When two applications/servers/ages/etc need to communicate, they tend to do so using strings, since strings can be interpreted in pretty much the same way in all ages. while a complex data structure is often described internally in terms of memory references, and represented with all kinds of curly, square, angle brackets or whitespace .. A string is just an ordered series of characters.
To this end, standard rules or syntaxes have been created to describe complex data structures as strings. JSON is one such syntax, as it can describe objects, arrays, strings, numbers, booleans and null in a string context, which can be passed from application to application and decoded as needed. yaml and XML (and even request Params) are two other popular Data Interchange (or serialization) Format S, but of course we like JSON because we're JavaScript developers!
Literals
And just for reference, I'm going to quote Mozilla Developer Center here:
- Literals are fixed values, not variables, that youLiterallyProvide in your script. (literals)
- A string literal is zero or more characters enclosed in double (") or single (') quotation marks. (string literals)
- An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). (Object literals)
So, when is JSON not JSON?
JSON was designed as a data interchange format, which happens to have a syntax that is a subset of JavaScript.
And as such,{"Prop": "Val "}
Cocould be a JavaScript Object literal or a JSON string, depending in what context it's being used. if it's used in a string context (surrounded by single or double quotes, loaded from a text file, etc) It is a JSON string. if it's used in an object literal context, it's an object literal.
//This is a JSON string.VaRFoo = '{"prop": "Val "}';//This is an object literal.VaRBar = {"prop": "Val "};
Also, note that JSON has a very strict syntax, and while{"Prop": "Val "}
Is valid JSON when used in a string context,{Prop: "Val "}
And{'Prop': 'val '}
Are not valid JSON. All property names and string values must be surrounded by double quotes, not single quotes! And for you PHP guys, note that escaped single quotes are also invalid. Yes, Flickr, I'm talking to you. Either way, seethe syntax rules for all of the details.
Putting things into context
So, all you smart-asses out there, you know who you are... you're thinking "Well, isn' t my JavaScript source code just one big string of text ?"
Yes, of course it is. all your JavaScript and HTML (among other things) are, by themselves, strings of text .. until they are interpreted by the browser. that. JS file or Inline JavaScript code ceases to be "just one big string of text" the moment that the browser interprets it as JavaScript source... Just like the page's innerhtml ceases to be "just one big string of text" as soon as it is converted into a DOM structure.
Again, it's all about context. use that curly-brace-delimited JavaScript Object in a string context and you 've got a JSON string. use it in an object literal context, and you 've got an object literal.
The JSON object
So, maybe I fibbed a little. while object literals are not "JSON objects," There really is a JSON object, but it's something else entirely. in modern browsers, The JSON object is a native object with the static methodsJSON. parse
(Deserialize a json string into an object) andJSON. stringify
(Serialize an object into a JSON string ). when you want to convert to and from JSON, you use these methods. in older browsers that don't provide a native JSON object, you can use json2.js to add this functionality.
And just in case you still don't understand, don't worry about it, here's a little cheat sheet:
//This is a JSON string, like what you 'd get back from an Ajax request.VaRMy_json_string = '{"prop": "Val "}';//This is how you deserialize that JSON string into an object.VaRMy_obj =JSON. parse (my_json_string); alert (my_obj.prop= 'Val ');//Alerts true, Fancy that!//And this is how you serialize an object into a JSON string.VaRMy_other_json_string = JSON. stringify (my_obj );
Also, Paul Irish pointed out to me that Douglas crockford uses the term "JSON object" in the json rfc, but in that context it means "object as represented in a JSON string" and not "Object literal."
Extra credit
If you want to learn more about JSON, here are a few notable JSON-related links, for perusal at your leisure.
- JSON Specification
- JSON RFC
- JSON on Wikipedia
- Jsonlint-The JSON validator
- JSON is not the same as JSON
See also:
Http://stackoverflow.com/questions/4538269/adding-removing-items-from-json-data-with-jquery
Http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation