Linux

JSON

JSON stands for JavaScript Object Notification. Its light weight data interchange and document. Douglas Crockford pioneered and popularized the use of JSON around 2000. JSON was coined for the first time by the state company co-founded by Douglas Crockford. The JSON arose out of the need of stateful, real time client browser interaction where requirement of flash and java applets was eliminated. But in today's world JSON has come into being used in more effective ways eliminating many dependencies. Any web based program can use standard javascript functions to convert JSON data into JavaScript objects.
Salient features of JSON:-
  1. Light-weight data interchange format.
  2. Easily readable for humans.
  3. Easy for machines to parse
  4. Multiple language support.
  5. No separate parser required for parsing JSON document.
  6. Developer friendly.
  7. More and more NoSQL Dbs are adopting for JSON Spec for storage of documents.
  8. Faster execution and development time.
A sample JSON document :-
    {"firstName":"John", "lastName":"Doe"},
    {
"firstName":"Anna", "lastName":"Smith"},
    {
"firstName":"Peter", "lastName":"Jones"}
]}

JSON VS XML

Both JSON and XML are used for data interchange among several web platforms. Both are used for carrying data in specified format. XML is basically a markup language and uses markup tags for data.
An example of XML Document:-

<Students>
    
<student>
        
<firstName>John</firstName> <lastName>Doe</lastName>
    
</student>
    
<student>
        
<firstName>Anna</firstName> <lastName>Smith</lastName>
    
</student>
    
<student>
        
<firstName>Peter</firstName> <lastName>Jones</lastName>
    
</student>
</students>

There are few similarities between JSON and XML:-
  1. Both are hierarchical.
  2. Both can be fetched using an XMLHttpRequest.
  3. Both support multiple languages.
  4. Both are easily read by humans.
  5. Both can be parsed easily by several languages.
But there are few key differences between JSON and XML documents worth noting.
JSONXML
Can be parsed using javascript function. No need for separate parserNeed separate parser to parse an xml document.
Much shorter document for same amount of data.Larger document for same amount of data.
No start or end tag for each entry.Uses end and start tag for each entry.
Is faster in execution.Takes some time in execution ( this time can be accounted for parsing the document)
More quicker to readTakes some time to read.
Arrays can be used.No such feature.
For Ajax applications JSON is faster and easier to use.Time consuming and needs separate parser first.

Typically every language follows the following steps to parse either of the documents: For xml document, program fetches the xml document then uses SAX or DOM parser to loop through the document and finally retrieves the data and stores in variables. For JSON Document program fetches the JSON string then parses it using JSON.Parse to retrieve the data.

JSON Syntax:-

{"students":[
    {
"firstName":"Bhaskar", "lastName":"Das"},
    {
"firstName":"Mohit", "lastName":"Raj"},
    {
"firstName":"Manish", "lastName":"Pundir"}
]}

  1. Each document starts with a curly braces and uses name/value pairs.
  2. As shown in the example above, Students is the JSON document name.
  3. Arrays are denoted by square bracket [].
  4. A JSON document can contain another document and so on.
  5. Data is separated by comma else it will throw error.
  6. Here objects are inside the curly braces namely as name/value pairs or key/value pairs. For e.g. "firstName":"Bhaskar" Here firstName is the name or key and "Bhaskar" is the value of the name or key.
  7. JSON values can be another document, array, string, integer, float, a Boolean and null value.
  8. JSON file type extension is .json while the MIME type is "application/JSON".
The new REST APIs are extensively making use of JSON documents faster data interchange and retrieval. Mongodb which is being discussed in our site is based on JSON style format for storage of data. It's easy to learn and easy to implement in the coming complex web-based programs.






admin