One of the great things about Python is the vast amount of libraries that are available to pull into your project – the resource at Vaults of Parnassus is a great place to start google code also tends to host lots of good python plugins.

The same applies to wrappers for popular web services such as the Amazon API, Facebook, Google Maps and Twitter – all of them provide out-of-the-box access tothese services without having to write a line of XML yourself.

Unfortunately not all libraries are built the same, and sometimes you just have to roll your own – this post is going to show how to use RESTful webservices the output JSON.

For those that aren’t in the know – JSON is short for JavaScript Object Notation, is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (coutrtesy of Wikipedia).

Basically it’s a simple way to send data back and forth as an object between sources – because it comes from JavaScript, it’s great for AJAX based services and JS interfaces that can pull data straight from a webservice onto the page.

But we’re not talking about Javascript – we’re talking about Django – Django has a built in ability to serialize and de-serialize Python objects to JSON and vice versa, it’s a handy feature that not only allows a Django site to send JSON requests to AJAX client-side code, but also a great tool for RESTful webservices that output JSON as opposed to the less-friendly XML.

The example I have below is a really simple mechanism for running a search through Twitter – something the python twitter wrapper doesn’t cater for and sites such as Twiturly rely on for their content. So, onto the code:

#This class holds the tweet - it's structured
# on purpose so we can easily access elements in
# a template tag
class tweet():
    def __init__(self, user, text, graphic, time):
        self.user = user
        self.text = text
        self.graphic = graphic
        self.time = time

# The meat of the operation
def search_twitter():
    search_url = 'http://search.twitter.com/search.json?q=Django'
    import simplejson as json
    import urllib
   
    raw = urllib.urlopen(search_url)
    js = raw.readlines()
    js_object = json.loads(js[0])
   
    #filter it all
    tweets = []
    for item in js_object['results']:
        user = item['from_user']
        graphic = item['profile_image_url']
        text = item['text']
        time = item['created_at']
       
        thistweet = tweet(user, text, graphic, time)
        tweets.append(thistweet)
   
    return tweets

Let’s just run through the elements:

  1. We use urllib to make the request to the search API – specify in the URL that we want JSON back
  2. We use the simplejson module to decode the reply
  3. urllib.urlopen(search_url) pens the request, we need to read the data to a variable in order to process it, that’s why we call the readlines() method
  4. The json.loads() method converts the json information from a string rather than a file
  5. We then run through the results of the JSON object using keys – I’d recommend running this through the interactive command window first to get an idea of the structure of the json
  6. I’ve used a class to hold the tweets becasue I wanted to be able to refence the elements quickly from template tags

That’s it – the simplejson module can also be used to serialise information back – but I think this comes into it’s element when working with AJAX calls – something nto in remit of this article.

Enjoy!