Readings

Lecture Notes Start Here

JSON

The more I learn about JavaScript, the more I love it. We can offload a lot of processing for web applications from the server to the browser, doing the coding in JavaScript instead of PHP, Python or Java.

But, in order to do the data processing with JavaScript, we need a good data representation that is easy to process in JavaScript. JSON is that notation.

With JSON, we can:

We did all but the last of these in Tandora, and we planned to do the last one (but just ran out of time). The result was lightning-fast searching and sorting.

JSON as a Data Structure

It's a hierarchical structure consisting of

JSON in Tandora

We'll take a few minutes to explore Tandora. Before we get to the code, here's the basic idea:

Okay, let's get a little glimpse of the code.

Same Origin Policy

Okay, so loading JSON data is easy and cool. But there's trouble getting data from different servers due to a browser security issue, namely the Same Origin Policy, which is that the browser will not grant an Ajax request to a different server.

Here's a variation on that human-readable version only this one tries to get the data from http://www.scott-anderson.org/demos/presentations.json, which is a different server:

We'll all look, using Firebug, at what happens in this case: the request is silently ignored. No error message, just no data.

JSONP

A clever work-around to the Same Origin Policy is called JSONP. It can make your head hurt, but here's the basic idea:

Here's a working example:

Here's the script element:

But, of course, that has no effect. Instead, we need to wrap the result in something, so we need a different PHP script:

Let's see it in action:

  1. data goes here

One last step: This is all well and good, but we'd like to dynamically create these JSONP requests. Well, we can once we realize that we can dynamically add script elements to a page. That's what the jQuery.getJSON() method does.

JSON and JavaScript

Not surprisingly, JSON is easy to handle in JavaScript:

JSON and PHP

We've seen that there is nice support for JSON notation in PHP

When traversing a JSON data structure, it's helpful to know about gettype,

JSON in Python

Similarly, there is good support for JSON notation in Python thanks to the JSON module.

This isn't too surprising, since a Python dictionary is the same thing as a JSON object, and all the other data (arrays, numbers, strings) translate easily.

For type checking in Python, try type(o) and isinstance(o,type)

JSON in Java

There are many packages for processing JSON in Java. One by Douglas Crockford himself is JSON in Java.

I wrote an example of it in action, traversing a JSON entity to pretty-print it.

The basic idea:

The code:

    cd ~cs304/pub/servlets/WEB-INF/classes
    java JSONpprint | more
  

Exercise on traversing JSON

Using your favorite language, write some pseudo-code for counting the number of atoms in a JSON data structure.

Once you can do this, you can do pretty much anything: search for something, modify the data structure, etc.

There are 6221 atoms in the Tandora book.

[an error occurred while processing this directive]