JSON vs XML

JSON and XML are human readable formats and are language independent. Both JSON and XML can be used to receive data from a web server.

The following JSON and XML examples both defines an employees object, with an array of 3 employees:

JSON Example
snippet
{
"employees": [
{
"firstName": "David",
"lastName": "Fernandas"
},
{
"firstName": "Clara",
"lastName": "James"
},
{
"firstName": "Katie",
"lastName": "Elizibeth"
}
]
}

XML Example
snippet
<employees>
<employee>
<firstName>David</firstName> <lastName>Fernandas</lastName>
</employee>
<employee>
<firstName>Clara</firstName> <lastName>James</lastName>
</employee>
<employee>
<firstName>Katie</firstName> <lastName>Elizibeth</lastName>
</employee>
</employees>

Similarities
XML and JSON are easily readable by both humans and machines(Self-Describing Data).
XML and JSON both use Unicode. So they both support Internationalization.
XML and JSON has the same interoperability potential.
XML and JSON can be used as an exchange format to enable users to move their data between similar applications.
XML and JSON are easily processed because the structure of the data is simple and standard.
XML and JSON are Open and extensible. XML’s one-of-a-kind open structure allows you to add other state-of-the-art elements when needed. This means that you can always adapt your system to embrace industry-specific vocabulary. Those vocabularies can be automatically converted to JSON, making migration from XML to JSON very straight forward.

Differences

XMLJSON
XML stands for Extensible Markup Language.JSON stands for JavaScript Object Notation.
XML is extensible. We can define new tags or attributes to represent data in it.JSON is not extensible because it is not a document markup language.
XML is object-oriented Actually, XML is document-oriented.JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.
XML separates the presentation of data from the structure of that data. XML requires translating the structure of the data into a document structure. This mapping can be complicated. XML structures are based on elements (which can be nested), attributes (which cannot), raw content text, entities, DTDs, and other meta structures.JSON structures are based on arrays and records. That is what data is made of.
XML documents can contain any imaginable data type - from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java
applets or ActiveX components.
JSON does not have a <[CDATA[]]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data. Besides, delivering executable programs in a data-interchange system could introduce dangerous security problems.
XML provides the display capabilities.JSON does not provide any display capabilities because it is not a document markup language.
XML is securedJSON is less secured than XML.
XML has to be parsed with an XML parser.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.
JSON is faster and easier than XML in AJAX applications.

Using XML
Fetch an XML document.
Use the XML DOM to loop through the document.
Extract values and store in variables.

Using JSON
Fetch a JSON string.
JSON.Parse the JSON string.
Related Tutorial