These are the golden days of JavaScript, which was warily used in the not too distant past because of the browser wars (still being waged). With enhancements to JavaScript in recent years and the advent of AJAX, interest in Javascript has taken a new turn, a turn for the better. Early on with AJAX it was recognized that there was a contender for XML for handling data which was stable, faster, and portable. This was the beginning of JSON.
JSON is minimal textual representation of data as compared to the "verbose" XML, which makes equivalent XML much larger than JSON. Well, both are textual, but JSON is more so than XML with all its "angularities." This is not to put down XML but just to highlight the obvious. This has obvious implications for AJAX, and more often JSON is the preferred data format in which AJAX results are received. Everything in XML is a string but JSON has types as discussed earlier.
JSON equivalent of XML
In my previous tutorials, XML Islands and XML Responses and AJAX, the file webstudents.xml was used for illustration. The same file will be represented as a JSON object in this section. The file is reproduced here for reference. In writing down the equivalence only the root element and its children are considered. Here is the listing of webstudents.xml.
<wclass>
<!--My students who took web programming class with me-->
<student id="1">
<name>Linda Jones</name>
<legacySkill>Access, VB5.0</legacySkill>
</student>
<student id="2">
<name>Adam Davidson</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
<student id="3">
<name>Charles Boyer</name>
<legacySkill>HTML, Photoshop</legacySkill>
</student>
<student id="4">
<name>Charles Mann</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
</wclass>
The rules in this conversion are simple. The root element will be the main object, and the three student nodes will form an array. Each array element will be a student object with its own attributes as a string value pair. If you keep the following syntax in your cross-hairs it will be easy to follow the listing.
Object:
var obj={key1:value1, key2:value2 ,key3:value3};
Array:
var Arr=[valueA, valueB, valueC];
The XML document has both simple and complex types and this is developed piecewise to make the explanation easy to understand.