Wednesday, August 12, 2009

JSON: JavaScript Object Notation

Todays news:

Business Week tells us Why Apple Is More Valuable Than Google

Executive summary: Google makes money on search. Loses money elsewhere. Apple makes money on everything.

JSON: JavaScript Object Notation:

I'm working on Rubies, plug-ins for Google SketchUp written in Ruby. More exactly, since the UI takes place in a browser, written in JavaScript and Ruby. Google has provided a mechanism for Ruby to pass a string to JavaScript and for JavaScript to pass a string to Ruby. I need to share lists of strings and an array of booleans for my Ruby. Enter JSON.

Like XML, JSON stores stuff in human-readable strings. Specifically, it stores objects, arrays, strings, numbers, and constants 'true', 'false' and 'null'. Strings and numbers are very much like strings and numbers in C (and from C, C++, Java and many others).

Arrays are a comma-separated list of values enclosed in square brackets. Array values may be anything that JSON supports: objects, arrays, ... (list above). Arrays may be all of a single type, or any mix of types. Arrays are one dimensional, but as arrays may be members of arrays, this is not a restriction.

Objects are zero or more comma-separated name:value pairs, enclosed in curly braces.

Add whitespace between values, if you like.

And that's it! Very light but no less capable than XML. If you want to write a JSON reader/writer, the exact details (such as the short list of escaped characters) are at www.json.org. A JSON reader/writer is already written for all the languages I know.

Doug Crockford invented JSON, sort of. JSON copies the notation Brendan Eich invented for JavaScript. Crockford noticed that this notation made a simple but full-powered version of XML. Here's an example:


pet={type:python, diet:vegetarian, TimToady:opposed}
pets = [ python, kitten ]


Those are valid JavaScript statements. To the right of the "=" signs you see valid JSON. I love things like this. We backpackers say, "When in doubt, leave it out."

JSON can get just about anything talking to anything else. Highly recommended.

No comments:

Post a Comment