Rose tinted json

Lately, we have been looking at providing a little automation to parts of the site which are less dynamic, by using some of the data serialisations that we create, with a sprinkling of javaScript magic.  Specifically we’ve looked at pulling Edge Hill event data, tagged with “rose theatre” into the Rose Theatre site.

This week I will be mostly using jsonCurrently events are added to the events Edge Hill events data, which can be duplicated. Our data serialisations come in four flavours, XML, JSON, PHP and YAML.

This week I have been mostly using JSON.

JSON is javaScript, and doesn’t need a server.  It runs on the site visitor’s browser (if enabled) and provides a way for us to access dynamic data and publish it on static web pages.

The first stage was to create the JSON feed.  Since Edge Hill already has data serialisations for Events and News, all I needed to do was to tweak it a bit and add a new function to the database to return events tagged with “rose theatre” and add a new URL to access the results which look something like this:

	"title":"Alternative Stand Up",
	"summary":"Comedy at the Rose Theatre.",
	"content":"<p>Tickets \u00a35.50 \/ \u00a33.50 concessions<p><strong>Jamie Sutherland<p>One of the freshest and most natural talents to emerge in the last few years, Jamie has truly established himself on the comedy circuit, and is now very much in demand all over the UK.\u00a0 Liverpool born Jamie will keep you amused with gags, tall tales, and true stories garnered from everyday observational topics.<p><strong>Danny McLoughlin<p>Danny is a complete natural and destined for the top.\u00a0 Think Peter Kay for the confidence factor.<p><strong>Rosie Wilby<p>Rosie Wilby has been performing as a singer songwriter for many years and turned to comedy in 2004, storming through to the semi finals of So You Think You're Funny, on only her second ever stand up gig.\u00a0 In August 2006, Rosie unveiled her debut full length show \u2018Olympic Swingball Champion 2012' at Edinburgh Fringe.<p>Compere.....<p><strong>Ste Porter<p>A top class act who will be a regular comedy circuit favourite before long.\u00a0 See him in Ormskirk first!<p>For further information or to book, call 01695 584480 or email <a>",
	"created_at":"2008-08-13 14:04:28",
	"updated_at":"2008-08-14 13:46:40",
		"Stand Up":"Stand Up",
		"rose theatre":"rose theatre"

Next, add the following code to the html document into which you want to add the data feed:

<div id="rtevents">
  <h3>Next up</h3>
    The Rose Theatre's <a href="/events/tag/rose theatre" title="List of events for the rose Theate">upcoming events</a>

The div with the id of “rtevents” acts as a placeholder for the code which will be inserted. At this point, a level 3 heading has been applied with a link to the events page. This gives visitors without javascript capabilities a way of viewing the events (in this case a link to events for the rose theatre page).

In addition to the tags, the jQuery library was added. jQuery has a ready-made method for “getting” a JSON feed and the documentation is detailed, yet simple enough to get you started with a working example.  Using that example, I wrote:

    $.getJSON(/events/tag/rose+theatre/json”, function(data){
      $(‘#rtevents’).html(<h3>Next up</h3>);
      $(‘#rtevents’).append(<table id="”eventrow”"></table>);
      $.each(data, function(i,data){
        urldate = data.start_date.substr(0, 4)+/+data.start_date.substr(5, 2)+/+data.start_date.substr(8, 2);
        date =<td>+data.start_date.substr(8, 2)+/+data.start_date.substr(5, 2)+</td>;
        time =<td>+data.start_time.substr(0, 5)+</td>;
        time = time ==<td>00:00</td>?<td>TBA</td>: time;
        oddeven = i % 2 == 0 ? ‘even’ : ‘odd’;
        $(“#eventrow”).append(<tr class="”‘+oddeven+‘”">+date+<td><a href="”/events/’+urldate+‘/’+data.slug+‘”">+data.title+</a></td>+time+</tr>);
        if (i == 5) {// sets the maximum number of records returned
          return false;

Let’s try to explain what’s going on here (row by row). The whole thing won’t work unless javascript is enabled in your browser.

  1. Line 1: A jQuery function which runs the javascript that follows it after the html page has loaded.
  2. Line 2: A jQuery call to the JSON feed and a function call to to tell the browser what to do with the data.
  3. Line 3: Find the element with the id “rtevents” and replace it’s contents with our <h3> element.
  4. Line 4-5: Append the table (named; “eventrow”) and table headers to the “rtevents” element (following the h3).
  5. Line 6: For each event returned in the JSON feed, do the following:
  6. Line 7-11: Format JSON data for output.
  7. Line 12: Append the table row to the table (named “eventrow”) with all the formatted data.
  8. Line 13-15: restrict the number of table rows returned if necessary.

Knowing this, you can parse any JSON feeds using jQuery, and there are plenty to choose from these days. A few examples:

Fancy yourself as a javaScript developer? Then add JSON to your bow strings.

Use our data

I mentioned previously about some of the feeds we’re providing for news and events and said at the end of the post that we’d be doing some stuff for the Institutional Web Management Workshop. Well it’s finally time that we have to firm up what that stuff is!

Edge Hill – along with the Universities of Aberdeen and Bath – are sponsoring the workshop’s Innovation Competition. For this we’re letting people know what data we have available for people to use in the creation of innovate, lightweight demonstrations of web technologies.

For the Big Brief we’ve restructured several of our systems to allow information to be extracted in different ways. Now as well as viewing web pages, you can use our data the way you want to or access in more machine-readable forms.

Several of these feeds we use internally – for example, the JSON feed of events is used to populate the calendar in the sidebar to show what days events are on.

It’s all work in progress so we’ll be adding more formats in the coming months but I’m really looking forward to seeing the ways people can use information from the website as part of the innovation competition.