Wednesday 30 May 2012

JSON...Son of JavaScript

I am really angry on my seniors.When I was a student at varsity level people are talking about server site programming language a lot,like php,Asp.net,JSP etc.For that , being a junior my focus on server site programming languages.But when I entered in my job sector it looks different.Developers trying to execute their code in client site as much as they can.One of our experience project manager told,"we need some client site programmer,because all my developers are experienced in server site coding but unable to perform well at client site."

Once I thought JavaScript is not so important in development sector but
now I realize how fool I am.It is not just important,its mandatory now.For Juniors,my suggest is,do JavaScript as much as you can.It makes you differ from others.

This article is not on "Importance of JavaScript".I am going to introduce on of my important friend on IT section named "JSON".And remember at first meeting you can't expect to know him very well.To know him such kind of way,meet him in regular process.Here,I just make an introduce with JSON. 

Pre requesting before you introduce:
Don't we put special dress to meet someone special.Have you dressed up?..
You require-
1.Little idea on JavaScript.Dont expect that you are rocket programmer on JavaScript.


Who is JSON?

JSON or JavaScript Object Notation,has been getting a large amount of attention recently in the IT world.It is a lightweight data-interchange format.That means, A format to exchange data from one end to another.Remember it,both end must need to know the format.If you dont understand yet, don't worry,I will give an example and surely you will understand it.For now just understand JSON is a data format which is use to interchange data.


How does it work?



To understand it clearly,Assume you have a online shopping site.Where every product has three attributes "Name","Available","Cost".Now you want develop an iPhone application (Remember it is an application not just browsing your website with browser) where you can see your products update information.
To get data from your website to your native application you need to request an API of your website and at your website end, it receive your request and reply your native application with data.Those data follow a format that is JSON format.And at your application site retrive those data by following this format.

Lets Create a JSON format :

All JSON objects are declared with a pair of curly braces. You can assign them to a variable like you would any other data structure.



var myObj = 

{
"products":[

  1. {
    • "Name":"Shirt",
    • "Available":true,
    • "cost":23
    },
  2. {
    • "Name":"Hat",
    • "Available":false,
    • "cost":24
    }

]
}


Here a JSON format and when application request an specific API, it will response with those data.

When you want to access it is quite easy
             alert( myObj[0].Name )   //"Shirt"
                 alert( [myObj[0]][Name] ) // "Shirt"

Two ways to access data.Its your wish which one you will choose.

Why its powerful :

JSON is capable to handle with integers,strings,arrays and booleans.It is most essential data building block and it is extremely fast transporting data between two interfaces.It is powerful and also easy to use.
But the power does not end there. Other popular programming languages have been implementing native support for JSON, such as PHP and ActionScript. For example, PHP can now convert an associative array of data into a JSON string by using the new json_encode() function, provided in version 5.2+ of the language. For languages without native support, you can find a full range of implementation classes available at the JSON website.

Fight between XML vs JSON :

JSON always win with huge vote.But I am not one side player.Lets compare with XML and JSON-

JSON :


Advantages:
  • Simple syntax, which results in less "markup" overhead compared to XML.
  • Easy to use with JavaScript as the markup is a subset of JS object literal notation and has the same basic data types as JavaScript.
Problem:
  • Simple syntax, only a handful of different data types is supported.



XML :

Advantage:
  • Generalized markup, it is possible to create "dialects" for any kind of purpose
  • XML Schema for datatype, structure validation. Makes it also possible to create new datatypes.
  • XSLT for transformation into different output formats.
  • XPath/XQuery for extracting information (which makes getting information in deeply nested structures much easier then with JSON).
Problem:
  • Relatively wordy compared to JSON (results in more data for the same amount of information).
At present in IT section people use JSON format because of user friendly and understand it quite easy.

Why I Like JSON :


1- lightweight data-interchange format 

2- Easy for humans to read and write 

3- Easy for machines to parse and generate 

4- JSON can be parsed trivially using the eval() procedure in JavaScript 

5- JSON Supports: ActionScript, C, C#, ColdFusion, E, Java, JavaScript, ML, Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Lua. 

Conclusion

JSON is simpler to get data from the server and use it as a persistent memory for a program. You have to know the structure of the data to use it, you must be the owner of the data file preferably.

It is lighter than XML and save resources.

XML is better for presentation purpose. It is the language of all graphical user interfaces for now: XAML, XUL, MXML, etc. In this case, the data is stored in a form and used in another form. 

You can use XML from external sources. The combination of XML and XPath allow to use XML as database, it is thus convenient for large resources.


Helpful Links : 







2 comments:

  1. Writting is a good habit...i encourage you to contnue this habit all the life, all the time. Wish your success

    ReplyDelete