Thursday, April 5, 2007

JSON vs XML

This article is intented to give a brief Sketch about JSON and a small comparison between JSON and XML

The JavaScript Object Notation, or JSON, is a lightweight syntax for representing data. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent.

JSON is a collection of name/value pairs and that is an Object. An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
The main advantage of JSON is that, it can be represented in any language standard say C, java and even in Scripts. The entire list is here.

I just tried CSS JSON, it was pretty easy. Here is one example. Say Suppose we have one CSS structure which goes like this.

selector-1 {
property-1:value1;
property-2:value2;
}

And how can we represented in JSON, it is really easy. Here it goes

{
“selector-1″:{
“property-1″:”value-1″,
“property-n”:”value-n”
}
}

Sounds easy???

Let us take another Ordinary XML schema and let us try representing them in JSON.

This is the XML version.
For example, an address book application might provide a Web service that yields address cards in this XML format:



Name
PH

abcd@xyz.com
me@mailme.com

+12345678
+1234 1234

1234 Oil Mill St
Chennai, IND

5678 Oil Mill St
Chennai, IND

http://techasilo.blogspot.com/
http://techasilo.blogspot.com/



Now we will analyse this XML, there is root entity called “card” and it has child nodes namely “fullname”,”org”,”telephone” etc. In turn they have

attributes. Let us take the innermost entity “telephones” it has attributes say “type” and “value”. So how can we represent them in JSON.
We can have name value pair arrays which goes like

“telephones”: [
{”type”: “work”, “pref”: 1, “value”: “+12345678″},
{”type”: “mobile”, “value”: “+1234 1234″}
],

So similarly it applies to all other elements and all the elements are contained in one OBJECT and we can draw them as

{
“fullname”: “Name”,
“org”: “PH”,
“emailaddrs”: [
{”type”: “work”, “value”: “abcd@xyz.com”},
{”type”: “home”, “pref”: 1, “value”: “me@mailme.com”}
],
“telephones”: [
{”type”: “work”, “pref”: 1, “value”: “+12345678″},
{”type”: “mobile”, “value”: “+1234 1234″}
],
“addresses”: [
{”type”: “work”, “format”: “us”,
“value”: “1234 Oil Mill St Chennai, IND”},
{”type”: “home”, “format”: “us”,
“value”: “5678 Oil Mill St Chennai, IND”}
],
“urls”: [
{”type”: “work”, “value”: “http://techasilo.blogspot.com/”},
{”type”: “home”, “value”: “http://techasilo.blogspot.com/”}
]
}

It is! It is sure that JSON will replace XML in future. But how?
Json is a notation; this raised some doubts for me. JSON is also a markup language like XML. Then what is the difference between JSON and XML.
-Both are text based.
-Both uses Unicode.
-Both are readable by humans.

But JSON has less redundancy, XML will be best for making documents and JSON can be for best interchange.

Why JSON would be a best way for interchange?
XML is not well suited to data-interchange because
*It carries a lot of baggage,
*It doesn’t match the data model of most programming languages.
And JSON overcomes all these disadvantages and still manages to provide interoperability and openness.

But one disadvantage is that, JSON is not extensible, but some says it doesn’t need to be, because they say JSON is not a Document markup Language, so it is not
necessary to define new tags and attribute to represent data in it and it doesn’t provide any display capabilities.

JSON might be the next Universal Standard.The next Generation of Technology

No comments: