What is JSON?
JSON stands for JavaScript Object Notation which is a data structuring format that is extremely clean and lightweight. Even though JSON is native to JavaScript (as in, it can be turned into an object directly by JavaScript), it is quite easy to handle with other languages, making it an ideal replacement of XML when JavaScript is involved with the exchange of data
JSON Features.
• Lightweight data-interchange format.
> Compared to XML
• Simple format.
> Easy for humans to read and write.
> Easy for machines to parse and generate.
• JSON is a text format
> Programming language independent.
> Uses conventions that are familiar to programmers of the C-family.
of languages, including C, C++, C#, Java, JavaScript, Perl, Python.
XML vs JSON
In my opinion, JSON should be used instead of xml, whenever JavaScript is on the receiving or sending end of the data. The reason for this is that when you use XML in JavaScript, you must write script or use libraries to handle the DOM objects to extract the data you need, whereas with JSON, it simply IS an object. This keeps overhead down and requires less CPU usage when preparing the data, not to mention it also decreases the amount of code you must write. There are many libraries out there for all the widely used languages for handling JSON with ease. This is debatable, but I also believe that even in a non-native language, parsing JSON will be faster due simply to a more lightweight data structure. In XML, there is a lot of bytes wasted and more memory required by the parser to keep track of tag names of varying sizes. I should also mention that some languages, such as PHP 5, actually have built-in libraries for handling JSON! Very cool!
Why Use JSON over XML
• Lighter and faster than XML as on-the-wire data format.
•JSON objects are typed while XML data is type less.
> JSON types: string, number, array, Boolean,
> XML data are all string
• Native data form for JavaScript code
> Data is readily accessible as JSON objects in your JavaScript code vs. XML data needed to be parsed and assigned to variables through tedious DOM APIs.
> Retrieving values is as easy as reading from an object property in your JavaScript code.
JSON Structures
• A collection of name/value pairs
> In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array
• An ordered list of values
> In most languages, this is realized as an array, vector, list, or sequence
•These are universal data structures supported by most modern programming languages.
Example: JSON Object
var myJSONObject = {“bindings”: [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
The (JSON) SYNTAX:
{
”tweet” : {”photoid” : “url”,“username” : “Qais_8822340”,
“message” : “Working in Assignment 2 & Practical Exam!”,
“date” : July 17, 2008”}
}
The (XML) SYNTAX:
<tweet>
<photoid>The URL Address</photoid>
<username> Qais_8822340</username>
<message> Working in Assignment 2 & Practical Exam!</message>
<date> July 17, 2008”</date>
</tweet>
Comparison XML vs JSON.
- XML parsing is very generic, you don’t have to care about the Types. JSON parsing on the other hand is very tricky.
- XML has the data representation and semantics attached to it. JSON is very good for data representation.
- Perhaps JSON data over the HTTP is faster than the XML.
- As a developer the code to parse XML is much cleaner than the code to parse JSON.
- Evaluating the JSON on JS is much faster and efficient. Due to its inherent support on JS, the learning cycle and manipulation of JSON data is faster than manipulation of XML by JS.
- For WEB2.0/RIA applications, where there is a frequent data transfer between the browser and server, it makes sense to get the response in JSON and manipulate it in the browser.
- I have used XML as domain models representation. It can contain the cross references, can contain types and attach semantics with the help of Schema.
- JSON does not have any namespaces. I am not sure if this is an added advantage for JSON.
- JSON Does not have validator. Developer is supposed to validate what ever he gets.
- I love the random access XML via XPath. Can i do the same thing in JSON.
- XLink, XQuery are some of the cool things i like on the XML.
- I can represent the entire design of a system as XMI. It has references, schema support, i can validate, etc…