0

Choosing JSON Over XML

I've never really done much with JSON.  I've known it existed, known how to use it and generally understood its benefits, but the truth is, I've just never really had a good reason to use it.  I've always been dealing with existing XML (i.e. integration with another system that required XML) or an XML-based standard (e.g. RDF/RSS/Atom).

Recently, though, I needed to whip up a quick interface to some backend work I was doing that returns a set of data-centric reports.  For the sake of simplicity, I decided to use AJaX to retrieve the report and format the return value using JSON rather than XML.

Here's what I like:

  1. It's smaller.  A lot smaller, in most cases.  In my case, I (unscientifically) estimated the JSON string to be about 50%-60% shorter than its XML counterpart would have been.  That can add up to a decent amount of bandwidth.
  2. It's less arbitrary.  No personal preference decisions over whether to place data in child elements or tag attributes.  An object/structure is annotated one way, an array another, simple values another.  Period.
  3. Many languages seem to have built-in, addon or available user-defined functions to encode and decode the JSON format.  For this project, I'm using a PEAR library for PHP ($ pear install json) and I've heard rumors that ColdFusion will offer native functions when Scorpio is released.
  4. ECMA/JavaScript handles JSON natively with the eval() function.  A quick call to eval ( myJSONVariable ) returns a very usable object that can be iterated through or randomly accessed.  Other JSON parsers are available for those concerned about the security of using eval().

Here's what I don't like:

  1. It's less human readable.  Not a big concern since there's rarely a need to inspect the raw data, but I felt like I needed something in the negative column.  :-)
  2. It's not quite as extensible as XML.  I mean, there's a reason there's an "X" in "XML", right?  If the response has to be handled by multiple disparate systems then XML is still the best solution.
  3. No formatting.  XML has XSLT, but no such thing exists for JSON.
There are still many situations where I think XML is the best choice, but I suspect I'll be using JSON for all of my non-trivial, non-XHTML return values.  Oh, and for any REST-style web services, as well.
tags:
Development
 
It sounds as if there could very well be some some native JSON functionality in Scorpio as well.
 
posted 572 days ago
Add Comment Reply to: this comment OR this thread
 
 
I've heard that (see #3 in the pros column) :-)
 
posted 572 days ago
Add Comment Reply to: this comment OR this thread
 
 
LOL somehow I stopped at PHP/PEAR and continued on. Man, you even took the time for individual linking and I blew right past it! Sorry.
 
posted 572 days ago
Add Comment Reply to: this comment OR this thread
 
 
I'm crushed right now, but I'll probably get over it in a few months.
 
posted 572 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Rob  Wilkerson