Projects


RSS to JSON Convertor

This script is capable of taking an RSS (or Atom) feed and converting it into a valid JSON object. I currently cache feed requests once per hour, to save on bandwidth - so keep that in mind when developing an application.

You can use the interface that I have setup, below, or you can download the script itself (written in Perl) and run it on your own server (which is preferable).

Interface

This script currently has a REST interface, accessible via a GET request. The full request would look something like this:
GET http://ejohn.org/apps/rss2json/?url=URL&callback=CALLBACK
the URL parameter would contain the URL of the RSS/Atom feed which you are attempting to convert. The optional Callback parameter would reference a callback function that you wish to have called, with the new data.

You can test this out by visiting the following URL:
http://ejohn.org/apps/rss2json/?url=http://ejohn.org/index.rdf

Sample Code and Demo

A simple, sample, program would look something like this:

getRSS("http://digg.com/rss/index.xml", handleRSS);

function handleRSS(rss) {
  alert( "Dowloaded: " + rss.title );
}

function getRSS(url, callback) {
  feedLoaded = callback;
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = "http://ejohn.org/apps/rss2json/?url=" + url
    + "&callback=feedLoaded&t=" + (new Date()).getTime();
  document.getElementsByTagName("head")[0].appendChild(script);
}

I've also setup a more advanced demo, which uses some tweaked code from the del.icio.us JSON page.

Also, the del.icio.us popular widget that I made using the Google Homepage API also uses the RSS to JSON convertor. Taking a quick peak at the source code shows it in use.

Download

If you wish to run your own copy of the RSS to JSON script, you can download it from here. It's a Perl-based CGI script which requires that the following modules be installed: LWP and XML::Feed.

This work is licensed under a Creative Commons Attribution 2.5 License.

Tags: javascript, json, perl, rss, convert

Current Projects

jQuery JavaScript Library

jQuery

Comprehensive DOM, Event, Animation, and Ajax JavaScript Library.

Recent Projects

Pro JavaScript Techniques

JavaScript Book

The best techniques for professional JavaScript. Published by Apress.


Hosting provided by the cool dudes at Engine Yard.