Skip to main content

JSON vs XML

Both JSON and XML can be used to receive data from a web server.

JSON and XML Example Comparison

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

  • 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 is Unlike XML Because

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

XML has to be parsed with an XML parser while 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.

The following table contains a comparison between JSON and XML:

JSONXML
JSON is easy to learn.XML is quite more complex to learn than JSON.
It is simple to read and write.It is more complex to read and write than JSON.
It is data-oriented.It is document-oriented.
JSON is less secure in comparison to XML.XML is highly secured.
It doesn't provide display capabilities.It provides the display capability because it is a markup language.
It supports the array.It doesn't support the array