How to use JSON ?

by Waqas 6/9/2008 12:49:00 PM

JSON is a textual data-interchange format. Its purpose is to offer a representation of structured data that is independent of the language or platform used. This makes it possible to interchange data between applications written in different languages and run the applications on different machines. Compared to XML, which is probably the best-known data-interchange format, JSON has a compact syntax. This means that often, less bandwidth is required to transmit JSON data through a network.

JSON is based on a subset of the JavaScript language. As a consequence, encoding and parsing are nearly immediate. JSON is built on two structures: a collection of name and value pairs, called an object; and an ordered list of values, called an array. In JSON, a value can be one of the following:

  • An object
  • An array
  • A number
  • A string
  • true
  • false
  • Null

An object is represented by a JavaScript object literal, and an array is represented by a JavaScript array literal. The remaining values are represented by the corresponding literals.

Because JSON is a subset of JavaScript literals, there are some restrictions on the syntax. In a JSON object, the name part of a name/value pair must be a string, and the value part must be one of the supported values. The following is the JSON representation of an object with two properties:

 

{ "firstName":"John", "lastName":"Doe" }

 

In JavaScript, both the objects have the same structure. However, the second object isn’t a valid JSON representation, because the names of the properties aren’t enclosed in double quotes. Restrictions also apply to JSON arrays, where elements must be supported values. For example, a Date object isn’t in the list of supported values and therefore can’t be an element of a JSON array or a property of a JSON object. A String has the same representation as a JavaScript string literal, except that strings must always be enclosed in double quotes. Numbers are similar to JavaScript number literals, but octal and hexadecimal formats aren’t supported. Here is an example of a JSON array:

 

[1, 2, 3, 5, 7]

 

The Boolean values true and false, as well as null, have the same representation as the corresponding JavaScript literals.

 

One of the advantages of JSON is that it’s easy to parse. Many JSON parsers, written for numerous languages, have been developed to automate the process of generating and parsing JSON. (A list is available at the official JSON site, http://json.org/.) In JavaScript, the parsing process is immediate: All you have to do is pass the JSON string to the JavaScript eval function. If you have a jsonString variable that contains the JSON data, the following code parses it and returns the corresponding JavaScript object:

 

var parsedJson = eval('(' + jsonString + ')');

 

Note that you should enclose the JSON data in parentheses before calling eval. By doing this, you force eval to consider the argument an expression, and an object literal {} won’t be interpreted as a code block. But the eval function can execute arbitrary code, which can lead to security issues if the data come from an untrusted source. For this reason, it’s always recommended that you validate the JSON data before calling the eval function.

 

Also note that the the official JSON site, http://json.org/, provides a regular expression for validating JSON data. You can find it in the JavaScript implementation downloadable from the website.

 

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

How To

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




Tags

© Copyright Beyond Web Logs.


Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in