JSON and JSONP: An Introduction to Javascript Data Interchange

JavaScript Object Notation or JSON for short is a data interchange format commonly used in AJAX Web Applications. The format is human readable and can represent simple data structures and Objects by use of associative arrays.

Syntax Formatting

The JSON format is lightweight in that the properties of the JSON Data Object are encoded by  curly braces. And the properties of the object are represented by key-value pairs delimited by the colon character and separated by a comma. In addition to an array value is defined by square brackets that can contain objects or values. Using these basic building blocks you are able to represent complex data.

Basic Examples

Example 1: Stock Information

{
    "symbol":"DELL",
    "open":"14.23",
    "lastTrade":"13:85"
}

Example 2: A Book Object with title, isbn, and author(s)

{
    "title": "First Lord's Fury",
    "isbn": "102304312",
    "authors": [
                 { "fullName": "Jim Butcher", 
                   "lastName":"Butcher", 
                   "firstName":"Jim" 
                 }
               ]
}

JSONP for cross Domain JSON Data

JavaScript Object Notation with Padding (JSONP) is a method that a server can implement for allowing other sites to access and utilized data from your server. Do to browser security it is not possible to access JSON data directly from a remove server. JSONP resolves this issue by allowing the caller to pass in a callback function that the JSON data gets wrapped in so that the data is executed by your defined function. The really nice element of this is that JQUERY and other JS libraries already contain AJAX support for you to easily create these calls.

JSONP server response format

For JSONP to function the server must except a parameter to be used as the callback function in addition to any data it needs to obtain the requested information.

http://www.example.com/?callback=XYZ

What the server needs to do is take the callback parameter and wrap the json data in a call to the callback function as:

XYZ( JSON-DATA )

That is all there is to it.

// Javascript //

Comments & Questions

Add Your Comment