8.2. 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 appears 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" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access