Published by nick on 25 Sep 2007 at 11:00 am
JSON vs XML vs serialize() for data
Who is this Jason guy and what does he want with our data? JSON stands for JavaScript Object Notation, and I think there are some pretty compelling reasons to use it all the time instead of php’s serialize() function, and maybe even to replace XML under some circumstances.
Why use JSON instead of XML for Asynchronous Javascript requests for your favourite web application? Well, why not? After all, it’s the simplest approach.
We’re going to build a widget for looking up the cities within 5 miles of a user supplied zip code. We want the user to enter their zip code into a form, and without reloading the page, use Javascript to go get data from our web service, and display it on the page. Let’s look at using JSON for the data type verses using XML.
JSON for web service
Server side:
$locations=getLocationsForZip($zip);
echo json_encode ($locations);
Client side:
var json=fetch(url);
var locations=eval(json);
XML approach
Server side:
$locations=getLocationsForZip($zip);
if (!empty($locations)){
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo "<locations>\n";
foreach ($locations as $location){
echo "<location>\n";
echo "<city>" . htmlspecialchars(utf8_encode($city)) . "</city>\n";
echo "<state>" . htmlspecialchars(utf8_encode($state)) . "</state>\n";
echo "<country>" . htmlspecialchars(utf8_encode($country)) . "</country>\n";
echo "</location>\n";
}
echo "</locations>\n";
}
Client side:
var xml=fetch(url);
// TODO: write nasty code for parsing XML into DOM
// More nasty code to iterate through the DOM object it to put it into a usable array.
Now, that makes the most sense for Asynchronous Javascript requests, but going further, does it make sense to do it even for normal data transport mechanisms?
Language Neutral Data Storage
We need to store data on the file system or in a database that needs to be programming language independent. Historically, XML has been the obvious choice for this task. I’ve seen some people use serialized PHP (yuck). You could roll your own pipe delimited or CSV kludge. I think JSON is a best choice.
- JSON is higher performance than XML, both in construction and parsing. Those of us who have [tried] to build scalable applications using XML/XSL have learned… not to.
- JSON is language neutral, and built in! Every major language now has a json encode/decode capabilities.
- JSON is simpler. Just run json_encode() via PHP, and then ‘eval’ in javascript, and you’re done. See above examples.
- JSON is more compact than XML. Less data on the disk, less data over the wire.
- JSON contains character set information, and handles encoding issues for you (for the most part) — PHP’s serialize does NOT–, and this will cause problems for you when you internationalize
- JSON maintains structure and objects, unlike pipe delimited or CSV hacks.
The more I use JSON for building my web applications, the more I’m finding that my reasons for using XML are fading away. Fast. XML is more difficult to deal with.
If you really feel compelled to use XML, do the rest of us a favor and make your webservice support both formats. Tip: Build a RESTful webservice and allow for a .json extension in your url in addition to .xml.
Props to Chris Cowan for helping me to see the light, and Douglas Crockford for evangelizing the use of Javascript.
-Nick
nick on 09 Nov 2007 at 11:00 am #
Note that Douglas Crawford at json.org (he also works at Yahoo!) is trying to come up with "jsonRequest" to replace xmlhttprequest. Makes sense to me.
http://json.org/JSONRequest.html
Jesse Donat on 14 Jan 2008 at 2:44 pm #
I was never a huge fan of XML, but I had just gotten into some higher level parsing of it when I stumbled across some Douglas Crawford videos on Yahoo, and they changed my life! heh, I am litterally in love with JSON
JSON vs. XML « PANVEGAs Blog on 10 Sep 2008 at 1:05 pm #
[…] http://www.techyouruniverse.com/software/json-vs-xml-vs-serialize-for-data […]
Stephan Schmidt on 10 Oct 2008 at 1:42 am #
"Serverside"
Use JAXB.
"// TODO: write nasty code for parsing XML into DOM"
Or use one of the available xml2json scripts
Peace
-stephan
steve on 11 Nov 2008 at 11:45 pm #
How can you make JSON support multi-dimensional data returns? Biggest advantage of xml has been the ability of nesting elements many levels deep. Very important when dealing with large scale reporting where it is not one independent record per line. If anyone has an answer, would be of great help.