Back to All posts

XML to JSON API Proxy for Quick Mash-Ups

May 2011

While building our company dashboard, which was mentioned in our previous blog post on SVG with Raphael, I came across a problem with the project management tool we use – Pivotal Tracker.

The Pivotal Tracker API is only in XML, which means you can’t do cross-domain requests and access the API via javascript. This meant that if I wanted to extract data about the status of our user stories, I would have to do it on the server side. As everything so far had been done with javascript on the client side, I was reluctant to start making things more complicated.

I contacted Pivotal, who said they might think about adding JSON at some point, but I didn’t want to wait for them to sort it out. Instead, I built a quick API proxy that allows you to make a request to a XML API and get back JSON. So, now, I can do this in my javascript:

$.ajax({
  type: "GET",
  url: 'http://xml2json.heroku.com',
  data:'url=https://www.pivotaltracker.com/services/v3/projects/'+projectId+'/iterations/current?token='+trackerToken,
  dataType: 'jsonp',
  success: function(data) {
    console.log(data);
  }
});

It will proxy the GET request to the API and convert the XML response into JSON, via http://xml2json.heroku.com (supports JSONP). I’ve put the code on GitHub http://github.com/bitzesty/xml2jsonp in case anyone else needs to do the same thing.