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:
- 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.
- 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.
- 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.
- 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:
- 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. :-)
- 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.
- No formatting. XML has XSLT, but no such thing exists for JSON.






Loading....