Saturday, September 7, 2013

HTML5: Parse and Generate JSON data using only Javascript, no external APIs.

JavaScript Object Notation (best known as JSON) is a lightweight data interchange format known to be compatible with almost every language in common use. It is sometimes used in JavaScript as a replacement for other transport formats, like XML.

JSON is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays.
  • An object is an unordered collection of zero or more name/value pairs.
  • An array is an ordered sequence of zero or more values.
  • The values can be strings, numbers, booleans, null, and these two structured types.

JSON can be used in Ajax requests or to store and load information on the client side.

ECMAScript 5 includes a native JSON object that is also included in other older browsers. This object allows us to convert standard objects, arrays, and JavaScript variables to a string JSON format using JSON.stringify() and to convert JSON string files to objects again using JSON.parse().

Most modern smartphone and tablet browsers support this object directly, and for older devices we can use a "Douglas Crockford" JavaScript polyfill.

Explore the following code on how those APIs could be used.