JSON (JavaScript Object Notation) is a data format that is non-proprietary, human-readable, easy to parse, simple to generate, and widely adopted. You can read more about JSON at the project website here http://www.json.org/ Some of you might be saying, wait there is another data format with the same properties, namely XML, why bother with JSON? For me there are two reasons for data JSON is superior to XML. First, it is designed for data so handles things such as arrays much more eloquently than XML. There are versions of XML better suited for Data than the generic flavor but on the whole, XML is trying to provide a flexible structure to describe complex metadata not be a simple format for data. Second, there is only one type of JSON whereas XML can come in endless variety.  It is XML’s flexibility that is both its asset and liability. By being flexible XML can be used in a variety of ways but because of this confusion often arises about exactly how to consume and generate data. One of the things XML was supposed to solve. When transferring data I do not like ambiguity, I want strict conventions.

A great article about XML verse JSON with good comments is found here, http://ajaxian.com/archives/json-vs-xml-the-debate.

But I use JSON not because it is better for data (there is always something better) but mainly because a lot of data I want to consume uses the JSON format and you can load it directly into R: http://cran.r-project.org/web/packages/rjson/index.html

Below is an example of a JSON formatted dataset:

"CustomerId" : 1,
"age" : 23,
"name" : "Joe"
"CustomerId" : 2,
"age" : 45,
"name" : "Mika"
"CustomerId" : 3,
"age" : 34,
"name" : "Lin"

You can see that it is human readable. {} enclose rows and a row/column is expressed as “Colname” : value.

Simulation Example

This month we are going to take a stab at programming a simple queuing model in JavaScript.  First, why JavaScript and not R or SAS?  JavaScript is a good language to use for programming examples because nearly anyone with a computer and browser can play around with it. Another good reason is that while JavaScript is not JAVA it is very similar to JAVA and to other languages such as C++ and C#.  Learning JavaScript will help you in coding in those languages as well.

The model:

We are going to code a very simple queuing model. For this simulation we are going to assume a single server (where the people are processed), that people arrive following a Poisson distribution and the service time is a random number with an exponential distribution.

See the code running here: example simple queuing model.

Code was written by Ted Harris, Dec 2007 for Analyticalway.com
Questions and comments to: questionandcomments@analyticalway.com
<script type= text/javascript >

First, we need to decide which distribution to use fro the arrival and service rates. Assuming a Poisson distribution for arrival times leads to an arrival rate generated from an exponential function. For service time we can also assume an exponential function.
Generating random number from an exponential functions is easy. We just need to solve: f(x) = -exp^alpha*x for x giving us: x = (-1/alpha) *log(f(x) ). Generate f(x) from a uniform distribution and you are done.

function exponential(intAlpha)
var intR = Math.random();
var intX = (-1/intAlpha) * Math.log(intR);
return Math.round(intX);

/* Here is the core to the code that runs the simuation.*/
function runsim ()

/*First we declare dynamic arrays to hold our cases and results. */
var aryCases = new Array();
var aryResults = new Array();
var intTotCases= 100; /* The total number of cases you want to simulate.. */
var fltArrival = .5; /* This variables sets the average arrival rate. */
var fltWait = .5; /* This sets the average wait time, */
/* Next we populate the arrival and wait times for each case. */

for (i = 0; i < intTotCases; i++)
aryCases[aryCases.length] = new Array(i, exponential(fltArrival) ,exponential(fltWait) );

Since we are assuming one server and one queue this is a relatively simple set up. That is dependant on the first case.
Now we can set the arrival and departure time for the first case. This on is simple:
Arrival = Exponential random number
Departure = Arrival + Exponential random number.

aryResults[aryResults.length] = new Array(1, aryCases[0][1] , aryCases[0][1] + aryCases[0][2] ,aryCases[0][1] ,aryCases[0][2] );

Now we set the arrival and departure times for the rest of the cases.
Arrival = Arrival time of previous queue member + Exponential random number
Departure = maxiumn value between Arrival + Exponential random number and exit time of previous queue memeber + Exponential random number .

for (i = 1; i < aryCases.length; i++)
aryResults[aryResults.length] = new Array(i, aryResults[i-1][1]+ aryCases[i][1] , Math.max( (aryResults[i-1][1]+ aryCases[i][1]+ aryCases[i][2]) , (aryResults[i-1][2]+ aryCases[i][2]) ) );

/* Now write the results to the webpage.

/* This function write the results. */
function writeresults( aryResults )

/*First, find the target object in the document (webpage) to over-write. */
var objParent= document.getElementById( results );
/* Create a new version of that object to be replaced.*/
var objNewParent = document.createElement( span );
/* Set the object ID to match that of the object we are replacing.*/
objNewParent.id = objParent.id;
/*Replace the object with our new object which has our new results writen to it. */
/*Now, clear out any child objects (content) associated with the old parent . */
while (objParent.firstChild)

/* Write the header to the output.*/
objNewParent.innerHTML += <p> Arrives , Departs </p> ;
/*Now, loop through the results table and write the contents to the new span we ahev created, */
for (i = 0; i < aryResults.length; i++)
objNewParent.innerHTML += <p> + aryResults[i][1] + , + aryResults[i][2] + </p> ;


<!– Create a button with an onclick event to run the code.–>
<button onclick = runsim(); > Run simulation </button>
<!– Add a break to make the code look a little better–>

<!– Create a span to write the output to. It is important to set the id for the span so JAVAScript can find it latter. –>
<span id = results >
</span >

Now you can play around with parameter and copy paste the output to Excel to do further research. In some version of Excel the columns will not be properly placed.  To correct for this copy paste the output to a text document then change the file extension to csv.  Excel should now open the file correctly. One fun thing to do is slowly increase the time to be served while keeping the arrival rate constant and watch how quickly the average wait time increases.

Further Reading:


Before you begin I would recommend getting Firefox and getting the add on firebug to test your code. This will make debugging a lot easier. Also the open source software NotePad++ http://notepad-plus.sourceforge.net/uk/site.htm makes a great editor.
First, lets create a html page.  Create a text file and change the file extension to HTML. Then right click on the file and choose open with>Word Pad   (or Notepad ++ if you have that).
Type in the following text:

<p> Hello World </p>

Save and double click on the file.  It should have been brought up in a browser and look like a rather simple webpage.

Now, in the Header section, between <HEAD> and </HEAD> add:

<script type = text/javascript LANGUAGE= JavaScript >
function helloagain()
alert( Well, Hello yourself. );

Then change
<p> Hello World </p>

<p onmouseover = helloagain() > Hello World </p>

Now save and open up in a browser.  When you mouse over the text a message box should appear.