JSON Basics - JSON and XML
(Page 4 of 5 )
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.
Next: Root element with students and their attributes >>
More JavaScript Articles
More By Jayaram Krishnaswamy