While XML seems to be the de facto choice for most data interchange these days, there are alternatives. One that
seems to be getting some attention lately is JSON (JavaScript Object
Notation). While it isn't XML-based, you may still find JSON's syntax familiar, because it's based on
JavaScript.
While there are implementations of JSON in just about every common language, one particularly easy implementation is
the JavaScript one—since the JSON data is valid JavaScript, a
simple eval statement is all you need to convert it into a JavaScript object.
As with many things in the field of technology, there are some nearly-religious arguments about which is the best
choice. Here's the official line on why JSON beats XML, at least
for data interchange. There are some good points there, although I disagree with parts—for example, it calls JSON "much
easier for humans to read than XML." I find XML much more human-readable than JSON myself, but your mileage may
vary. Mark Nottingham has a more
even-handed look at how JSON stacks up to XML. [via
Simon]
Sidestepping the religious wars, over the next few days I'm going to take a closer look at JSON and some of the things
it can do, and try creating some simple implementations myself. Stay tuned.
JSON: Lightweight data interchange
Reader Comments
(Page 1)2. If you load the javascript in a separate frame you don't have to parse or eval it. The browser will run it like any other inline javascript. XmlHttpRequest will not parse and run javascript.
Posted at 5:49AM on Dec 19th 2005 by Dan Sickles
3. "
XmlHttpRequest will not parse and run javascript
"
And thank goodness for that.
FYI, check out the results of this script (suitably embedded in an html doc or external script, of course:
======================
function foo() {
this.bar = 1;
}
var test = new foo();
var test2 = {bar: 1};
alert('test.bar=' + test.bar);
alert('test["bar"]=' + test['bar']);
alert('test2.bar=' + test2.bar);
======================
Posted at 5:49AM on Dec 19th 2005 by Jeremy Dunck
4. I think what Dan meant was that you could do something like this (ASP example):
objstr = "var myObj={'foo':'bar'}"
Response.Write "<script>"
Response.Write objstr
Response.Write "</script>"
And that would execute. Putting it in a hidden frame would mean that you have to reference the frame to get to the object (document.frames['myframe'].etc..) But it could be done like that. Personally I'd rather just do the eval to keep things simple.
Posted at 5:49AM on Dec 19th 2005 by Bart Miller
5. In case anyone is interested, here is an example of something similar to JSON called Japano. It uses Java Server Pages and Javascript. If you look at the source, you can see that it can be adapted for use with any server-side language:
http://logotopia.net/japano/
Posted at 5:49AM on Dec 19th 2005 by Bart Miller
6. Bart,
I knew what Dan meant. I was just responding that it's good that XMLHTTPResponse doesn't try to do something special with non-XML text.
And the code snippet I posted is just to show the strong ArrayObject tie in JS, for those that weren't aware of it.
Posted at 5:49AM on Dec 19th 2005 by Jeremy Dunck
7. XMLHTTPRequest itself may not "parse" javascript but nothing stops you from sending a string back down the wire and 'eval()' ing it. Actually quite handy...
Posted at 5:49AM on Dec 19th 2005 by Mike Watkins
8. JSON/Japano uses only a very small set of the stuff mentioned on the JSON site.
It's basically just a tool class which generates a JSON representation of a nested java object graph.
The main benefit IMHO is that you get a clear symmetry between server side and javascript object code.
The property "bar" of the serverside bean object "foo" can be accessed on the clientside like this
function handleFoo(foo)
{
alert(foo.bar);
}
(The japano CVS version is somewhat more advanced than the current online demo. need to do another release ASAP.)
This should work similar for all kinds server side languages.
Posted at 5:49AM on Dec 19th 2005 by Sven Helmberger








1. This Crockford guy has helped me out a lot with the articles he has written on object-oriented javascript. This JSON concept he has developed just reinforces my belief in the power of javascript.
Posted at 5:49AM on Dec 19th 2005 by Bart Miller