The following quote is derived from an expert recommended book that covers the many aspects of Ajax. It’s called “Ajax in Action”, authored by Dave Crane, Eric Pascarello, and Darren James.
“The JavaScript Object Notation (JSON) is a core feature of the language [JavaScript]. It provides a concise mechanism for creating arrays and object graphs. In order to understand JSON, we need to know how JavaScript arrays work […]”
Hopefully you’ve spotted the dodgy parts of that quote. For one, JSON, the data-interchange format is not actually a core feature of JavaScript and is, in more ways that one, quite different to JavaScript’s object notation (i.e. the way you markup an object literal in JavaScript code).
Despite my dismay I continued to read onwards, and I can tell you that the author has also made the classic mistake of thinking that JavaScript supports associative arrays, and for some reason thinks that you have to understand JavaScript arrays in order to understand JSON… (???)
So, just to set the record straight, JSON is an interchange format created/coined by Doug Crockford. It’s a subset of JavaScript’s object notation (specified in the ECMA-262 specification). To prove this, here’s a valid JavaScript object literal:
{ foo: 123, bar: 456 } |
Yes, I know, on its own it’s not valid JavaScript (since it’s interpreted as a compound statement), but that doesn’t matter; it is a valid object literal.
Run that through a JSON validator and you’ll get errors. That’s because JSON is not the same as JavaScript’s object notation. To make the above code a valid piece of JSON you’d have to wrap each property name (e.g. foo
) with double quotes:
{ "foo": 123, "bar": 456 } |
I don’t blame the author of that book. In all likelihood, the information within that book was thought of as 100% correct at the time of writing. I only wanted to set-straight one of many misconceptions surrounding JavaScript and its “subsets”.
PS. Please forgive the confusing title.
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
I did consider buying this book, but it just seems really outdated now and verbose. Is this a small mistake in the context of the book’s size, or one of a series of small mistakes amounting to Big confusement? 😛
Yes, that’s really an annoying thing that so much people think that Javascript has associative arrays…go back to your server-side if you want really associative arrays 😉
Javascripts object literal is really a cool feature of this language!
You are not the first to mention the problem with object literal and JSON talk. If you do enough Google searches you will see that.
Book came out in Oct. 2005, believe the JSON Spec [RFC 4627 application/json] came out in July 2006?? [do not quote me on that 🙂 ].
The book lead the way when it first came out and it is now becoming a dinosaur as the technology progressed just like any tech book that was the first released when the technology was new.