JSON - better than XML?
02 July 2006 - JavaScript
JavaScript coders have been doing it for ages. Now it's got a name: JSON, or JavaScript Object Notation.
One of the sexier features of JavaScript is a shorthand notations for defining and populating a data structure containing objects and arrays. So, instead of:
me = new Object() ;
me.name = 'Nick' ;
me.age = 30 ;
me.hobby = new Array()
me.hobby[0] = 'Reading' ;
me.hobby[1] = 'Writing' ;
me.smelly = false ;
You can do this...
me = {
name : 'Nick' ,
age : 30 ,
hobby : ['Reading', 'TV'] ,
smelly : false
} ;
Try doing that in PHP. This is almost the equivalent of the XML:
<me>
<name>Nick</name>
<age>30</age>
<hobby>Reading</hobby>
<hobby>TV</hobby>
<smelly>false</smelly>
</me>
Ideally, when you get XML data from a server, you'd have a standard function to automatically parse the XML and convert it into a data structure just like one in JavaScript above.
But there's a problem here: a standard function like this doesn't know whether to expect just one, or more than one, tag of each type to appear. Which means it doesn't know whether to represent an item as a scalar type (string, boolean, integer, etc), or as an array. Looking at the XML code itself doesn't help, because even if there's only one hobby in there, we'd still like it to come back as an array.
More over, we don't which of the scalar types apply to each of the tags. In the case of name, and maybe age, this isn't a problem. But with smelly, this is: if we assume it is the string, "false", instead of the boolean FALSE, JavaScript will treat the value of smelly as true is a boolean context.
The only solution with XML is to write a custom wrapper for each XML schema to convert it into a correct data structure. What a waste of time. Twice over: once for parsing the XML, and again for writing loads of wrappers.
Why not skip the XML, and just send the raw JavaScript notation? It's much more transparent to other languages, containing more 'hinting' information about the types uses in the structure; it's fairly easily parsed; and, of course, the total size of the data sent or stored is considerably smaller.
Head over to http://www.json.org for some JSON specs, compilers and parsers.


Comments
Edemilson Lima - 17 November 2006 22:35
There is a good article about this at:
http://www.json.org/xml.html
I my humble opinion, JSON is far better than XML.
Matt Burgess - 25 November 2007 23:28 - Visit >
JSON is a great standard. I've added support for it to several APIs I've made as it cuts out the middle-man, reduces complexity, and makes development much faster and simpler. Creation of JSON code with PHP is trivially easy, so there's no reason not to do it.
The only issue I'd take with this is "try this with PHP".
Well, OK:
$me = array( 'name' => 'Nick' , 'age' =>30 , 'hobby' => array('Reading', 'TV') , 'smelly' => false ) ;
Neither difficult nor particularly different. The only question really is whether you refer to creating it in PHP as an OBJECT specificically. Because... yeah, that would be tricky. But comparing a PHP object to a JS object is fairly pointless.