JSON or XML intermediate data format

You might or might not have heard about XML and JSON. And wonder which one is better to represent data with for communication, save functionality or config.

Before I do so I will explain what JSON and XML are in depth. If you are not interested, skip to ‘One over the other’

What is JSON?
JSON stands for JavaScript Object Notation and is actual javascript code. When put in to a javascript evaluator JSON can directly be translated in to objects, array and primitive types (integer, string, float).

A basic JSON string containing a person object could look like this:

{
name: ‘John’,
age: 14,
length: 1.55,
profession: ‘Student’,
friends: ['Suzzy', 'Cate', 'Mike', 'Tom']
}

Although JSON can only be parsed when well formed. Their is no default possibility to define what every value should look like. If you want to put ‘hello world’ into the length property this is valid. To do such checks code should be written to check these properties.

As noted before that JSON is actual javascript code. So parsing of these string is in general very quick.

What is XML?
XML stands for Extensible Markup Language and has been standardized by the W3C (World Wide Web Consortium). And has been created to standardize the way HTML (HyperText Markup Language) is written.

The same object shown above could look like this.


	John
	14
	1.55
	Student
	
		Suzzy
		Cate
		Mike
		Tom
	

I would like to point out that this representation automatically makes ‘objects’ from friends. Make it easier to put more properties on these. But the same can be done in JSON. So far you can represent the same complexity of data.

XML however has 2 things that make it more powerful then JSON.

First off is XSD (XML Schema Document) can be used to validate the structure and data types (not if it’s well formed, because it needs to be well formed to be able to check it against an XSD). XSD are XML’s with tight regulation on how they should be build and describe how your XML should look like. Thus using an XSD makes it very easy to pinpoint troubles in your data structure as it will give an accurate error of the problem.
For further information check: http://www.w3schools.com/schema/default.asp

Second is XSLT (EXtensible Stylesheet Language) which can be used to format a XML to more readable formats. Although only text can be produced by default, like HTML pages or txt files. This format is also tightly standardized and is being used as a base for PDF generators and other formats.
Check http://wiht.link/XSLT-intro/ for more information.

Finally I would like to point out that XML needs some getting used to when first used to write a data structure for. With JSON objects and arrays explain themselves. Defining structures comes naturally as it is clear what every part does.
With XML it might not always be clear how something should be represented at first and some research is needed. This is especially true when using XSD or XSLT documents.

One over the other
Here under I will point out the strengths and weaknesses of both formats.

Using JSON over other interchange-formats
Pro:

  • Can be read more quickly
  • Little overhead
  • Fast writing
  • Easier to learn
  • High performance deserializing

Cons:

  • Needs time getting used to
  • If you write your own JSON parser its hard to debug
  • Harder to cut in to chunks

Thoughts on XML
Pro:

  • Better error handling on syntax errors
  • More easily readable with deep levels
  • Superiour data integrity checking when XSD is used
  • XSLT can be used to formating for readability

Con:

  • To make error handling work their is need to make a XSD
  • Overhead with closing tags (slows down reading too)
  • Chance of conflicting names and functions of nodes
  • Necessity to create well formed XML constructions to represent your data
  • Harder to learn when structures become more complex

Conclusion
Although you might be dazzled or tempted to choose one over the other. I would like to point out that neither is absolutely superior and either can be used for all purposes you might have to represent data. However one is more suited for certain applications then the other.

If you have a website with lose data calls and no refresh, use JSON. It’s faster and has less overhead. In this fashion JSON is suitable for other communication and cases you need small size and high decoding speed.

If you need readability and tight error control over large sets of data that are being maintained by people. Use XML. It’s XSD data integrity checking makes it far easier to write by hand and XSLT can speed up human checking. So it’s ideal for config files, documents and dumps of large chunks of data that are complex in nature.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply

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