JSON stands for JavaScript Object Notation. JSON objects are used for transferring data between server and client, XML serves the same purpose.

JSON is a minimal, readable format for structuring data.

Features of JSON:

  1. It is light-weight
  2. It is language independent
  3. Easy to read and write
  4. Text based, human readable data exchange format

 

Much Like XML Because:

  1. Both JSON and XML is "self describing" (human readable)
  2. Both JSON and XML is hierarchical (values within values)
  3. Both JSON and XML can be parsed and used by lots of programming languages
  4. Both JSON and XML can be fetched with an XMLHttpRequest

Much Unlike XML Because:

  1. JSON doesn't use end tag
  2. JSON is shorter
  3. JSON is quicker to read and write
  4. JSON can use arrays

The biggest difference is:

XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.

Why JSON?

For AJAX applications, JSON is faster and easier than XML.

Using XML

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

Using JSON

  1. Fetch a JSON string
  2. JSON.Parse the JSON string

JSON Syntax Rules

JSON syntax is derived from JavaScript object notation syntax:

  1. Data is in name/value pairs
  2. Data is separated by commas
  3. Curly braces hold objects
  4. Square brackets hold arrays

JSON Files

  1. The file type for JSON files is ".json"
  2. The MIME type for JSON text is "application/json"

The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object.