Tuesday, July 22, 2008

JSON or XML?

Which is the better format for data representation? JSON? or XML? That is a tough one because there are several dimensions here that constitute better. Some easy differences:
  • XML is easier to understand, and therefore better for humans to read.
  • JSON is more lightweight, and therefor better for software to read.
So how do you go about deciding which format to go with in the context of your application? If the data is constantly manipulated by humans, the answer is easy; XML. In this scenario, the lightweight of JSON simply doesn't pay off. If the data is seldom interpreted by humans but the data format is transferred across a network (which it most likely will), JSON would be the way to go.

Can the two data formats exist within the same context? Yes. Would it make sense to use both formats in the same application? Not likely. One exception I can think of is when using third-party libraries or tools that require one format or the other but you have already invested heavily in the other format. The two formats are isomorphic enough that there is no magic needed to convert between the two. It is just unnecessary if it can be avoided.

If I were to start developing an application from scratch today, and the choice between the two formats needed to be made, I would most likely choose JSON. There are few reasons to not use it. There exist JSON libraries for all the major programming languages. The only question is human interpretation. It would be pretty easy to build a JSON, tree-style, viewer/editor (I've never seen anything out there that does this, and only this). This JSON editor/viewer will be a topic of further discussion in later posts.