General Articles
A Comparison – Json vs Xml
Both XML and JSON are general purpose, extensible and self descriptive languages. Rather than languages I would like to call them as specifications because they just define standards for data representation. After the introducing xml to the world, it spread ed like a storm. For the moment XML had every thing. It was easy to use, self descriptive and xml documents were more readable for people who don’t have a good knowledge in programming languages. So xml was used to act in many areas. Mainly in web services and other configurational documents (such as deployment descriptos and build file in application servers like JBoss). And also xml was heavily used to implement data transfer protocols(eg:XMMP). Using xml programmers can transmit an object from one place to other. Technology they used was take all the properties from the object, represent them as xml tags in a xml document. Then transmit the document to another place and reconstruct the object using the tags of xml document.
Here is a example: Say you have following java class:
class User{
String name;
int age;
}
And assume we have all the getters and setter implemented and there is more other customs methods. Say there is a particular user object which is named as John and age of 30 years. Notice that according to the definition, object is an instance of an class. And we use attributes of each object to differentiate among different objects. So if we want to reform an object in a different place, all you need is the set of attributes. So as I mentioned earlier we can use xml to transfer those attributes. So for the above user object xml document may be like this,
<User>
<Name>John</Name>
<Age>30</Age>
</User>
There are a lot of xml parsers and open source libraries available for any programming language. Where you can convert an object to a xml document and xml document to and an object. Today xml is used by many personals, organization and technologies for various purposes.
On the other hand Json also has the same set of goals and attributes of xml. But Json uses key value pairs to represent an object. As resonantly introduced Josn is still spreading. As an example if we use Json to represent above User object it may be like this;
{“Name”:”John”,”Age”:”30″}
So as you can see one of major disadvantage of xml was it uses a large number of characters to describe the data. But josn uses just a few. This makes json much faster and effective than xml in data transferring. And programming languages can process json much faster and effectively. Often people tends to user xml with Ajax. But with Ajax, josn is more effective(So we may call Ajaj instead of Ajax). There is a java script function called eval() which can be used to evaluate a string statement. So if you use eval with a Json string, immediately output is an json object. So it is very easy to use. And many modern javascript libraries provides much more compatibility with json.
So Json is the best person and xml is worse. No.. Xml has some qualities that json cannot reach. One of the is readability. Xml documents are more readable and self descriptive than json. Those who familiar with Ant, can imagine what will be your experience if you have to write and Ant build file in json? It will be a disaster. And same applies to deployment descriptors.
So finally both xml and json has good and bad qualities. It is up to you to choose the best one according to your requirement.
I hope this helps. Thank you.
Excellent buddy… Keep it up…!