JSON

JSON : Javascript Object Notation

JSON syntax is a subset of the JavaScript object notation syntax:

– Data is in name/value pairs
– Data is separated by commas
– Curly braces hold objects
– Square brackets hold arrays

JSON values can be:

A number (integer or floating point)
A string (in double quotes)
A Boolean (true or false)
An array (in square brackets)
An object (in curly braces)
null

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "height_cm": 167.6,
  "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"
    }
  ],
  "children": [],
  "spouse": null
}

Advantages of JSON:
– JSON is more compact
– can be easily loaded in JavaScript. (but using eval() in JS is security risk. So use a language json parser like JSON.parse).
– JSON parsing is generally faster than XML parsing
– Formatted JSON is generally easier to read than formatted XML.
– JSON specifies how to represent complex datatypes, there is no single best way to represent a data structure in XML.
– More structural information in the document
— Can easily distinguish between the number 1 and the string “1” as numbers, strings (and Booleans) are represented differently in JSON.
— Can easily distinguish between single items and collections of size one (using JSON arrays).
Easier to represent a null value

Advantages of XML:
– XML tends to have broader tool support and is more easily queryable (for example, using XPath)
– XML is stricter
– has support for schemas – I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.
– has support for namespaces – I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.
– XML is best used when transporting something like a patient chart or text document with markup included
– XML has XSLT and XPath. So you have your data in one format but you want it in another. If the data is in XML, you can write an XSLT template and run it over the XML to output the data into another format. Using XPath, it’s possible to get direct access to a part of multiple parts of an XML data structure
– JSON is not expressive enough to separate attributes from values. XML can do that.

Leave a Reply