just one more geek in a sea of austin techies

June 26, 2011

JSON bigger than XML??? #MarkupGeek

Continuing to chip away at my little Android programming projects, this week I put together some code to consume data from a JSON-formatted web feed.  JSON is more streamlined than XML and also happens to avoid certain cross-domain security roadblocks.  This means JSON is often a great choice for encapsulating objects and data that are destined for transport via web services over cell phone data plans.  It was a surprise, therefore, when I unexpectedly ran across an example of XML that was less verbose (smaller!) than its JSON equivalent...

The last few weeks I've been playing with Basic4Android, a neat little IDE that presents a VB-like language and uses the actual Android emulator as the designer for visual elements.  This week I used the HttpClient library along with the JSON library to create a program that consumes JSON-formatted web feeds.  Since JSON is more streamlined than XML, at some point I decided to compare some JSON-encoded data against equivalent XML-encoded data to see the space-savings.  To save some time I borrowed from a Google JSON example that presented the same data as both XML and JSON.  I minimized both sets of data (trimmed whitespace, removed newline characters, etc) and then took a character-count of each data set.  And then I did a double-take:  the XML was smaller than the equivalent JSON.  Huh?

Conventional thinking says that JSON should be smaller than equivalent XML, not the other way around.  It's easy to see how XML will typically exhibit more bloat than JSON:

XML (73 characters):

JSON (51 characters):


I do believe this basic premise to hold true more often than not:  XML-encoded data requires more space than JSON-encoded data.  In case you find the topic (and the exception I ran across) to be as interesting as I do, here are links to the code and tools I used for the comparisons:

Google example page (with source XML and JSON data sets):

Note:  Interestingly, the Google-provided JSON example code is invalid due to a couple of small-but-critical errors.  A copy of corrected JSON code appears down below if you want to copy-and-paste your way through your own tests.

JSON code minimizer (must select the "compact" option in the dropdown box):

XML code minimizer:

Character counter:

XML:  2096 characters
JSON:  2203 characters

Corrected JSON code (already minimized):
{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"","xmlns$openSearch":"","xmlns$gd":"","xmlns$gCal":"","id":{"$t":"..."},"updated":{"$t":"2006-11-12T21:25:30.000Z"},"title":{"type":"text","$t":"Google Developer Events"},"subtitle":{"type":"text","$t":"The calendar contains information about upcoming developer conferences at which Google will be speaking, along with other developer-related events."},"link":[{"rel":"...","type":"application/atom+xml","href":"..."},{"rel":"self","type":"application/atom+xml","href":"..."}],"author":[{"name":{"$t":"Google Developer Calendar"},"email":{"$t":""}}],"generator":{"version":"1.0","uri":"","$t":"Google Calendar"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"25"},"gCal$timezone":{"value":"America/Los_Angeles"},"entry":[{"id":{"$t":"..."},"published":{"$t":"2006-11-12T21:25:30.000Z"},"updated":{"$t":"2006-11-12T21:25:30.000Z"},"category":[{"scheme":"...","term":"..."}],"title":{"type":"text","$t":"WebmasterWorld PubCon 2006: Google Developer Tools in General"},"content":{"type":"text","$t":"Google is sponsoring at <a href=''>WebmasterWorld PubCon 2006</a>.\nCome and visit us at the booth or join us for an evening demo reception where we will be talking '5 ways to enhance your website with Google Code'.\nAfter all,\nit is Vegas, baby! See you soon."},"link":[{"rel":"alternate","type":"text/html","href":"...","title":"alternate"},{"rel":"self","type":"application/atom+xml","href":"..."}],"author":[{"name":{"$t":"Google Developer Calendar"},"email":{"$t":""}}],"gd$transparency":{"value":""},"gd$eventStatus":{"value":""},"gd$comments":{"gd$feedLink":{"href":"..."}},"gCal$sendEventNotifications":{"value":"true"},"gd$when":[{"startTime":"2006-11-15","endTime":"2006-11-17","gd$reminder":[{"minutes":"10"}]}],"gd$where":[{"valueString":"3150 Paradise Road,Las Vegas,NV 89109"}]}]}}

No comments:

Post a Comment