logo

JSON - Comparison With XML


Show

Both JSON and XML may be used to acquire information from a web server.

The following JSON and XML examples each define an employees object, with an array of three employees:

Example of JSON

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

Example of 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>

Common things in JSON and XML

  • Both JSON and XML are "self-describing" (human-readable)
  • Both JSON and XML are hierarchical (values inside values)
  • Both JSON and XML may be parsed and utilized by lots of programming languages
  • Both JSON and XML may be fetched with an XMLHttpRequest

Non-Common things in JSON and XML

  • JSON would not use stop tag
  • JSON is shorter
  • JSON is faster to examine and write
  • JSON can use arrays

The largest distinction is:

XML needs to be parsed with an XML parser. JSON may be parsed via means of a popular JavaScript function.

Why JSON is good than XML

XML is a good deal and more difficult to parse than JSON.

JSON is parsed right into a ready-to-use JavaScript object.

For AJAX applications, JSON is quicker and easier than XML:

Using XML

  • Fetch an XML document
  • Use the XML DOM to loop via the document
  • Extract values and keep in variables

Using JSON

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

JSON and XML are human-readable formats and are language-independent. They each have guides for creation, reading, and decoding in actual global situations. We can compare JSON with XML, primarily based totally on the subsequent factors −

Verbose

XML is greater verbose than JSON, so it's miles quicker to jot down JSON for programmers.

Arrays Usage

XML is used to explain the established data, which would not consist of arrays while JSON consists of arrays.

Parsing

JavaScript's eval approach parses JSON. When carried out to JSON, eval returns the defined object.

Below is the short and clear differentiation of JSON and XML

JSON


1. It is JavaScript Object Notation


2. It is a way of representing objects.



3. Its files are very easy to read as compared to XML


4. It doesn’t support comments.

XML


It is an Extensible markup language


It is a markup language and uses a tag structure to represent data items.


Its documents are comparatively difficult to read and interpret.


It supports comments.