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
Salient features of JSON:-
- Light-weight data interchange format.
- Easily readable for humans.
- Easy for machines to parse
- Multiple language support.
- No separate parser required for parsing JSON document.
- Developer friendly.
- More and more NoSQL Dbs are adopting for JSON Spec for storage of documents.
- Faster execution and development time.
A sample JSON document :-
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:-
There are few similarities between JSON and XML:-
- Both are hierarchical.
- Both can be fetched using an XMLHttpRequest.
- Both support multiple languages.
- Both are easily read by humans.
- Both can be parsed easily by several languages.
But there are few key differences between JSON and XML documents worth noting.
|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 read||Takes 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.
- Each document starts with a curly braces and uses name/value pairs.
- As shown in the example above, Students is the JSON document name.
- Arrays are denoted by square bracket .
- A JSON document can contain another document and so on.
- Data is separated by comma else it will throw error.
- 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.
- JSON values can be another document, array, string, integer, float, a Boolean and null value.
- 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.