JSON vs XML


JSON and XML are both used to get data from a web server.


The given JSON and XML examples describe an object for employees. This object includes an array containing three employees.

JSON Example

{"employees":[
  { "firstName":"John", "lastName":"Doe" },
  { "firstName":"Anna", "lastName":"Smith" },
  { "firstName":"Peter", "lastName":"Jones" }
]}

XML Example

<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 Like XML Because

  • JSON and XML are both easy to understand for people.
  • JSON and XML both organize data in layers, with values nested within other values.
  • JSON and XML can be read and utilized by many programming languages.
  • You can use an XMLHttpRequest to get both JSON and XML data.

JSON is Unlike XML Because

  • JSON doesn't require an end tag.
  • JSON is brief.
  • JSON is faster to understand and create.
  • JSON can utilize arrays.

The main contrast is:

XML needs an XML parser for parsing, while JSON can be parsed using a regular JavaScript function.


Why JSON is Better Than XML

XML is harder to understand than JSON.
JSON becomes a usable JavaScript object after being understood.

In AJAX apps, JSON is quicker and simpler than XML.

Using XML

  • Retrieve an XML file
  • Utilize the XML DOM to iterate through the document.
  • Retrieve data and save it in variables.

Using JSON

  • Retrieve a JSON string.
  • Parse the JSON string using JSON.Parse.