XML vs JSON

XML vs JSON

 
XML and JSON are two of the most popular data interchange formats.

Refer below a comparison between the two using an example that has employee details represented as an xml and as a json.
 

XML

<?xml version="1.0" encoding="UTF-8" ?>
<employee>
  <firstName>John</firstName>
  <lastName>Smith</lastName>
  <age>25</age>
  <address>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021-3100</postalCode>
  </address>
  <phoneNumbers>
    <type>home</type>
    <number>212 555-1234</number>
  </phoneNumbers>
  <phoneNumbers>
    <type>office</type>
    <number>646 555-4567</number>
  </phoneNumbers>
</employee>
  

 

JSON

 

{
  "employee": {
    "firstName": "John",
    "lastName": "Smith",
    "age": "25",
    "address": {
      "streetAddress": "21 2nd Street",
      "city": "New York",
      "state": "NY",
      "postalCode": "10021-3100"
    },
    "phoneNumbers": [
      {
        "type": "home",
        "number": "212 555-1234"
      },
      {
        "type": "office",
        "number": "646 555-4567"
      }
    ]
  }
}

 

XML and JSON similarities

 
Both XML and JSON can store nested data in a hierarchical manner. For example, see how phone data is stored within employee.

Both XML and JSON are commonly used data interchange formats.

Both are self describing in nature and readable.
 

XML advantages

 
XML attributes can store metadata about elements. For example, we can write person as :


<employee sex="Female">
...
</employee>

JSON can not have attributes.. we have to add the same as elements.

We can use XSLT to transform XML documents into formats like HTML and PDF.

XML is a markup language and allow users to create any tags needed and then describe those tags and their permitted uses.
 

JSON advantages

 
JSON is light-weight and less verbose compared to XML.

JSON’s representation of objects and arrays enables direct mapping to data structures in languages like JavaScript, Java, C# etc.

We need XML parsers to parse XMLs, but since JSON is a subset of JavaScript, we can parse it using standard JavaScript functions like parse and eval.
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags