JSON vs XML

Many of you have probably wondered which format is better or which should be used. You might have even participated in a full blown nerd rage war on the matter.

Both formats are good when you want to describe a hierarchical structure.

Differences

  • XML is a markup language and is good for documents description or text documents with formatting information.
  • JSON syntax is light. It’s much easier to parse from JavaScript and that’s why it’s preferred for browser-based and Phonegap mobile applications.

So when should you use each one of them?

  • If you are creating a web service which will be used with a JavaScript driven front-end then it’s better to go with JSON. It’s native for JavaScript so you’ll be spending less time on parsing.
  • If, for some reason, your web service returns XML and the front-end is written with JavaScript then you can just use the default DOM parser. If, however, you are not familiar/comfortable with it or you are using some cool fancy `XML to JSON` scripts then you are doing it wrong and should probably change your server code to return JSON. This will improve the parsing performance of your code and will reduce the total JavaScript libraries that get loaded at startup.
  • If your front-end is an Apache Flex app or a Starling game then XML is the right choice. Action Script 3 XML parser is really powerful and nicely working.
  • XML is great when your data contains styling information like text style or formatting.
  • If you need a really really complex data structure with information coming from lots of sources then the XML format together with namespaces is the best choice. This way you’ll also avoid naming clashes.
  • XML is slightly more readable from human beings than JSON. If the content will be static and edited by Muggles (not programmers) then go with XML. But if, and only if, you have a bunch of Muggles who are well trained in the art of magic basics, you can also choose JSON.
  • If the bandwidth is of high importance for you then the XML with its close/open tags (container tags) will take up unnecessary resources. JSON on the other hand is just a simple key/value pairs which will lead to less traffic.

Conclusion

Both formats have their pros and cons. The choice really depends on the technologies you’ll be using and the specifics of the data. My guess is that in most cases you’ll be working with JSON, but there are still some XML islands here and there.

I, personally, am secretly obsessed with using the XML plus XSLT combo, so rejoice fellow elderly orcs! XML is still alive…ish.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.