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:
- It is light-weight
- It is language independent
- Easy to read and write
- Text based, human readable data exchange format
Much Like XML Because:
- Both JSON and XML is "self describing" (human readable)
- Both JSON and XML is hierarchical (values within values)
- Both JSON and XML can be parsed and used by lots of programming languages
- Both JSON and XML can be fetched with an XMLHttpRequest
Much Unlike XML Because:
- JSON doesn't use end tag
- JSON is shorter
- JSON is quicker to read and write
- 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
- 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
JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax:
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
JSON Files
- The file type for JSON files is ".json"
- 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.