What is JSON

Course- Javascript >

Earlier in the tutorial you saw how to directly instantiate an object using the new Object() syntax. In this section you learn about JavaScript Object Notation (JSON), which, as its name implies, offers another way to create object instances, and which can also act as a general-purpose data exchange syntax.

The official home of JSON is at http://json.org/, which also has links to a wide variety of JSON resources on the Web.


JSON (pronounced “Jason”) is a simple and compact notation for JavaScript objects. Once expressed in JSON, objects can easily be converted to strings to be stored and transmitted (across networks or between applications, for instance).

However, the real beauty of JSON is that an object expressed in JSON is really just expressed in normal JavaScript code. You therefore take advantage of “automatic” parsing in JavaScript; you can just have JavaScript interpret the contents of a JSON string as code, with no extra parsers or converters.

JSON Syntax

JSON data is expressed as a sequence of parameter and value pairs, each pair using a colon character to separate parameter from value. These "parameter":"value" pairs are themselves separated by commas:

"param1":"value1", "param2":"value2", "param3":"value3"

Finally, the whole sequence is enclosed between curly braces to form a JSON object representing your data

var jsonObject = {
"param1":"value1",
"param2":"value2",
"param3":"value3"
}                    
                    

The object jsonObject defined here uses a subset of standard JavaScript notation— it’s just a little piece of valid JavaScript code.

Objects written using JSON notation can have properties and methods accessed directly using the usual dot notation:

alert(jsonObject.param1); // alerts 'value1'

More generally, though, JSON is a general-purpose syntax for exchanging data in a string format. Not only objects, but ANY data that can be expressed as a series of parameter:value pairs can be expressed in JSON notation. It is then easy to convert the JSON object into a string by a process known as serialization; serialized data is convenient for storage or transmission around networks. You’ll see how to serialize a JSON object later in this section.

Note: As a general-purpose data exchange syntax, JSON can be used somewhat like XML, though JSON can be simpler for humans to read. Also, the parsing of large XML files can be a slow process, whereas JSON gives your script a JavaScript object, ready to use.

JSON has gathered momentum recently because it offers several important advantages. JSON is

  • Easy to read for both people and computers
  • Simple in concept—a JSON object is nothing more than a series of parameter:value pairs enclosed by curly braces
  • Largely self-documenting
  • Fast to create and parse
  • A subset of JavaScript, meaning that no special interpreters or other additional packages are necessary

A number of leading online services and APIs including Flickr, Twitter, and several services from Google and Yahoo! now offer data encoded using JSON notation.

See http://www.flickr.com/services/api/response.json.html for details of how Flickr supports JSON.