JSON by isbangee


Mike Peat Unicorn InterGlobal
Synergy 2009

What is JSON?
lightweight computer data-interchange format (http://www.json.org)  About the simplest thing you can get beyond CSV format  “Discovered” by Douglas Crockford  Based on JavaScript’s “object literal” notation

JavaScript Object Literals???

In JavaScript you can create objects in a couple of ways:
1. var myObj = new Object(); 2. var myObj = {};


These are totally equivalent, but #2 is called an “object literal”

JavaScript Object Literals???
object can have properties  For an object literal, we add these directly between the curly braces as name/value pairs, separated by commas: var myObj = { “firstName”: “Mike”, “lastName”: “Peat” };
 An

JavaScript Object Literals???
 Then

in JavaScript code we can access those properties using JavaScript’s dot notation:

if (myObj.lastName == “Peat” && myObj.firstName == “Mike”) { var buyHimBeer = true; }

What makes up JSON?

There are only six “kinds” of data in JSON:
1. 2. 3. 4. 5. 6. Strings – “xyz” … “Hello Mike!” (NB: double-quotes) Numbers – 17 … -22.76425 … 8.54621e7 Booleans – true or false Null – null Objects – { “propName”: value, … } Arrays – [ value, value, value… ]

 

Where “values” can be any of the six “kinds” of data That’s it! (Did I say it was simple?)

So what does it look like?
{ “msgID”: 14358, “message”: “Can I buy you a beer Mike?”, “sent”: “2009-03-18 15:21:17.271”, “addressee”: { “fName”: “Mike”, “sName”: “Peat” }, “intList”: [346, 82, -118, 1.234e7, 17, -56], “buyHimBeer”: true }

A little more complexity…
To allow for special characters to be handled, “\” is used to “escape” some characters in strings…
\” \\ \/ \b \f double-quote back-slash slash backspace formfeed \n \r \t \uFFFF newline carriage-return tab 4 hex digit value

“discovered” JSON in April 2001 (although he claims others also “found” it independently)  2002 Crockford put up a description on json.org  People rapidly started adopting the format  July 2006: RFC 4627 formalised JSON as a standard  Mime type “application/json”
 Crockford

Why should I care?
compact than XML for network transmission  Much better match for programming language data structures – removes the “impedance mismatch” of the XML DOM  Much faster to translate into those programming data structures than XML  Much more “natural” to use in JavaScript than XML
 More

Why is that?
is much faster (and indeed much more natural) than XML because…  of the DOM (the Document Object Model)  The DOM was designed, I suspect, not by morons (as is often thought)…  But by a committee of pathologically evil geniuses who wanted to destroy software developer productivity the world over
 (Which  JSON

worked! )

Is JSON better than XML?
not for everything…  But when a program on one end wants to transfer data to a program on the other end…  And they both already understand the structure of the data…  Such as when you are building an Ajax application feeding off a web service under your control…  Yes it is
 Well,

Where can I use JSON?
you have a (web) service which returns JSON data…   JavaScript is the most obvious place to use JSON  Typically in Ajax web applications  However there are JSON capabilities or toolkits in many languages - see http://www.json.org for a pretty long list
 If

How do I use JSON?
JavaScript, JSON data can be extracted into native data structures using “eval”…  DON’T DO THIS!!!
 “eval”  In

is extremely dangerous!


eval( “( doSomethingVeryBad(); )” ); Will result in SomethingVeryBad happening!

How do I use JSON?
use a JSON parser  Many exist, but the canonical one is probably Douglas Crockford’s json2.js (from http://www.json.org/json2.js)  With that included in your HTML, your JavaScript can then (safely!) just say:
 Instead

var oMyData = JSON.parse(sJSON);

How do I use JSON?
you are using one of the JavaScript libraries, these will generally have their own JSON parsers and assemblers  In ExtJS (for example) we would use: var oMyData = Ext.util.JSON.decode(sJSON);
 If

Why does this matter?
 Ajax-style

web applications are increasingly looking like (one of) the future(s) of application development:
  

Web 2.0 Software as a Service (SaaS) Cloud Computing

 Ajax

uses JavaScript and data transfer from the server

Why does this matter?
will be no escaping JavaScript anytime soon…  Even if something better was introduced tomorrow and implemented in all major browsers overnight… (yeah, right!)  There would still be browsers out there in widespread use that didn’t support it five years from now… but which did support JavaScript
 There

Why does this matter?
is the natural way to serve data to JavaScript applications (it essentially is JavaScript data)  The commonest JavaScript toolkits tend to make using JSON very easy (and XML really quite hard!)  Translating XML into JavaScript data is also very slow by comparison to JSON

Why does this matter?
get on the bandwagon of developing these next generation applications…  JavaScript is an inevitable part of the toolkit  And transferring data to that JavaScript is a major part of the game  And JSON is the best way to do that
 To

One minor issue…
course, using JSON instead of XML in Ajax applications raises a problem…  Ajax stands for Asynchronous JavaScript and XML  If we use JSON instead of XML it becomes “AJAJ” instead…
…  Of

which doesn’t sound nearly so cool!


Thank you! Any Questions?

To top