What Is JSON?
JSON is a very lightweight data format based on a subset of the JavaScript syntax, namely array and object literals. Because it uses JavaScript syntax, JSON definitions can be included within JavaScript files and accessed without the extra parsing that comes along with XML-based languages. But before you can use JSON, it's important to understand the specific JavaScript syntax for array and object literals.
Array Literals
For those unfamiliar with JavaScript literal notation, array literals are specified by using square brackets ([ and ]) to enclose a comma-delimited list of JavaScript values (meaning a string, number, Boolean, or null value), such as:
var aNames = ["Benjamin", "Michael", "Scott"];
You can then access the values in the array by using the array name and bracket notation:
alert(aNames[0]); //outputs "Benjamin"
alert(aNames[1]); //outputs "Michael"
alert(aNames[2]); //outputs "Scott"
Note that the first position in the array is 0, and the value in that position is "Benjamin".
Because arrays in JavaScript are not typed, they can be used to store any number of different data types:
var aValues = ["string", 24, true, null];
This array contains a string, followed by a number, followed by a Boolean, followed by a null value. This is completely legal and perfectly fine JavaScript.
If you were to define an array without using literal notation, you would have to use the Array constructor, such as:
var aValues = new Array("string", 24, true, null);
JSON versus XML
As mentioned previously, one of the advantages of JSON over XML is that it's more compact. XML is considered by some to be overly verbose for its purpose. But what does this mean exactly? Consider the following XML data:
<classinfo>
<students>
<student>
<name>Michael Smith</name>
<average>99.5</average>
<age>17</age>
<graduating>true</graduating>
</student>
<student>
<name>Steve Johnson</name>
<average>34.87</average>
<age>17</age>
<graduating>false</graduating>
</student>
<student>
<name>Rebecca Young</name>
<average>89.6</average>
<age>18</age>
<graduating>true</graduating>
</student>
</students>
</classinfo>
This example contains information about three students in a class. Right away, there is some XML information that isn't entirely necessary: the <classinfo> and <students/> elements. These elements help to define the overall structure and meaning of the information, but the actual information you're interested in is the students and their information. Plus, for each piece of information about the students, the name of the information is repeated twice, although the actual data is repeated only once (for example, "name" appears both in <name> and </name>. Consider the same information formatted as JSON:
{ "classinfo" :
{
"students" : [
{
"name" : "Michael Smith",
"average" : 99.5,
"age" : 17,
"graduating" : true
},
{
"name" : "Steve Johnson",
"average" : 34.87,
"age" : 17,
"graduating" : false
},
{
"name" : "Rebecca Young",
"average" : 89.6,
"age" : 18,
"graduating" : true
}
]
}
}
As you can see, a lot of the superfluous information isn't present. Since closing tags aren't necessary to match opening tags, it greatly reduces the number of bytes needed to transmit the same information. Not including spaces, the JSON data is 224 bytes, whereas the comparable XML data is 365 bytes, saving more than 100 bytes. (This is why Crockford, JSON's creator, calls it the "fat free alternative to XML.")
The disadvantage to JSON-formatted data as compared to XML is that it's less readable to the layperson. Because XML is verbose, it's fairly easy to understand what data is being represented. JSON, with its shorthand notation, can be difficult to decipher using the naked eye. Of course, an argument can be made that data exchange formats should never be viewed with the naked eye. If you're using tools to create and parse the data being passed back and forth, then there is really no reason to have the data be human readable. But this begs the question: Are there any JSON tools available? The answer is yes.
You can find a number of JSON tools for server-side languages, so no matter what your preference is, you can use the power of JSON:
- C#/.NET: The C# JSON library, written by Douglas Crockford, is available at www.crockford.com/JSON/cs/.
- ColdFusion: The CFJSON library, written by Jehiah Czebotar, is available at http://jehiah.com/projects/cfjson/.
- Java: The JSON in Java utilities, written by Douglas Crockford, are available at www.crockford.com/JSON/java/. Can be used in JSP.
- Perl: The JSON library, written by Makamaka Hannyaharamitu, is available at http://search.cpan.org/dist/JSON/.
- PHP: In addition to JSON-PHP, there is also php-json, a C extension for PHP written by Omar Kilani and available at www.aurore.net/projects/php-json/. You must be comfortable with compiling PHP with extensions.
- Python: The json-py library, written by Patrick D. Logan, is available at https://sourceforge.net/projects/json-py/.
Douglas Crockford also maintains a fairly comprehensive list of JSON utilities at www.crockford.com/JSON/index.html. Check there before searching for JSON utilities for any other language.
Technorati Profile
Tags: json