HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JSON vs XML

Both JSON and XML are used to receive data from a web server. They share many similarities, but JSON has become the standard for modern web development because of its efficiency and ease of use in JavaScript.


Side-by-Side Comparison

Compare how the same employee data is structured in both formats:

XML Format
JSON Format
<employees>
  <employee>
    <name>John</name>
    <city>NY</city>
  </employee>
  <employee>
    <name>Anna</name>
    <city>London</city>
  </employee>
</employees>
{
  "employees": [
    { "name": "John", "city": "NY" },
    { "name": "Anna", "city": "London" }
  ]
}

How are they Similar?

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

What are the Differences?

Feature XML JSON
End Tags Mandatory for every element None
Size Bulky and verbose Short and compact
Arrays Difficult to represent Native support for []
Parsing Needs an XML Parser (slow) Browser-native JSON.parse() (fast)

Why JSON is Better for AJAX

XML is much more difficult to parse than JSON. For AJAX applications, JSON provides a significant performance boost:

XML Workflow
  1. Fetch an XML document.
  2. Use the XML DOM to loop through the document.
  3. Extract values and store in variables.
JSON Workflow
  1. Fetch a JSON string.
  2. JSON.parse(string).
  3. Done! Data is ready to use as an object.

Key Points to Remember

  • JSON is shorter and quicker to read/write.
  • JSON can use arrays; XML cannot easily.
  • The biggest difference is that XML has to be parsed with an XML parser, while JSON can be parsed by a standard JavaScript function.
  • JSON is preferred for Data Interchange.
  • XML is still used for Document Structure and configuration files (like Android XML layouts).