SPOILER ALERT

JSON Wins!
But learn and understand both layouts. You will find that JSON is adopted more, its lightweight and easier on the human eye.

What is XML?

Extensible Markup Language (XML) is simple to read. It's a flexible text format derived from SGML (ISO 8879). Originally designed to meet the challenges of large-scale electronic publishing, XML was the gold standard for data exchange on the Web and elsewhere

Simple XML might look like this:


<?xml version="1.0" encoding="windows-1252" standalone="yes"?>
<Records>
<Record>
<Row A="first" B="surname" C="website" />
</Record>
<Record>
<Row A="Nick" B="Litten" C="www.nicklitten.com" />
</Record>
<Records>

Pros:

  • Mark-up code of XML is easy to understand for a human; it is possible to create "dialects" for any kind of purpose
  • XML is an extensible markup language like HTML
  • XML tags are not predefined. You need to define your customized tags
  • XML Schema for datatype, structure validation. Makes it also possible to create new datatypes
  • built in support for namespaces

Cons:

  • Relatively wordy compared to JSON (results in more data for the same amount of information)

What is JSON?

JavaScript Object Notation or JSON (/JAY-sən), is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types. It is a very common data format used for asynchronous browser/server communication, including as a replacement for XML in some AJAX-style systems.

Data types in JSON include string, number, boolean, array

Simple Json might look like this:

{
  "name": "Nick",
  "surname": "Litten",
  "website": "www.nicklitten.com"
}

Pros:

  • Simple syntax, which results in less "markup" overhead compared to XML.
  • Easy to use with JavaScript as the markup is a subset of JS object literal notation and has the same basic data types as JavaScript.
  • JSON Schema for description and datatype and structure validation
  • JsonPath for extracting information in deeply nested structures

Cons:

  • Simple syntax, only a handful of different data types are supported.
  • No support for comments


In Summary


Use JSON if you need a lightweight, easy-to-read format that integrates well with JavaScript and is commonly used in web development. Use XML if you need strong schema support, hierarchical data organization, and cross-platform interoperability.

Having said that - for a typical webservice I recommend JSON because:

JSON is simpler:

  • only supports UTF-8, whereas XML supports a variety of encodings
  • doesn't support schemas, transformations
  • doesn't support namespaces
  • The method of "escaping" data is considerably simpler.

JSON is faster

  • On average, it is approximately 70% smaller than XML in size.
  • Simplicity leads to faster parsing.
  • Simple to use in JavaScript.
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>