JSON and XML

JSON is Like XML

  • Both JSON and XML are “self describing” (human readable)
  • Both JSON and XML are hierarchical (values within values)
  • Both JSON and XML can be parsed and used by lots of programming languages
  • Both JSON and XML can be fetched with an XMLHttpRequest

JSON Example

{“employees”:[
{“firstName”:”Adam”, “lastName”:”Amazing” },
{“firstName”:”Mary”, “lastName”:”Marvelous”},{“firstName”:”Peter”, “lastName”:”Perfection”}
]}

XML

<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>

JSON is different than XML

  • JSON doesn’t use end tag
  • JSON is shorter
  • JSON is quicker to read and write
  • JSON can use arrays

The biggest difference is that XML must be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.

Why JSON is Better Than XML

  • XML is much more difficult to parse than JSON.
  • JSON is parsed into a ready-to-use JavaScript object.
  • For AJAX applications, JSON is faster and easier than XML.

Using XML

  • Fetch an XML document
  • Use the XML DOM to loop through the document
  • Extract values and store in variables

Using JSON

  • Fetch a JSON string
  • JSON.Parse the JSON string