By Craig Buckler

Ajax and Web Service Data Formats Part 3: Custom Responses

By Craig Buckler

This is the last article in a series discussing web service data formats for Ajax. Part 1 examined XML, SOAP and HTML and Part 2 discussed JSON and JSONP. Today, we look at custom data formats.

The most efficient data transmission formats use a minimal number of structural elements to delimit the data. Consider our book data in XML and JSON. We’re always expecting six items of data per book, so we could simply use a format such as:

The Principles of Beautiful Web Design, 2nd Edition;;Jason Beaird;SitePoint;39.95;USD
jQuery: Novice to Ninja;;JEarle Castledine & Craig Sharkie;SitePoint;29.95;USD
Build Your Own Database Driven Website;;Kevin Yank;SitePoint;39.95;USD

Essentially, our data is similar to comma-separated list. We’ve used carriage returns to delimit books and semi-colons to delimit individual data items (commas couldn’t be used because they appear in book titles and possibly numeric values). Choosing the right data delimiters is the most important decision we have to make.

Unlike JSON, we must extract and parse data from the returned string, but this can be achieved quickly and easily using JavaScript’s string split() method. The following code will convert the data above to an identical JSON format:

// convert custom data to an array of JavaScript objects
function ParseBookData(ajaxdata) {
	var book = [], bookData = ajaxdata.split("n"), bookItem;
	for (var b=0, bl=bookData.length; b < bl; b++) {
		bookItem = bookData[b].split(";");
		book[b] = {
			title: bookItem[0],
			url: bookItem[1],
			author: bookItem[2],
			publisher: bookItem[3],
			price: {
				amount: parseFloat(bookItem[4]),
				currency: bookItem[5]

	return book;

var book = ParseBookData(xhr.responseText);
alert(book[0].title); // first book title
alert(book[1].url); // second book URL

JavaScript can process the data very quickly — even if thousands of books are returned. In most cases, you’ll find that total time for downloading and parsing the data is less than the equivalent JSON-powered Ajax code.

A custom data format offers several advantages:

  • It’s the most lightweight format and will result in faster Ajax responses.
  • The data can be generated quickly by any server-side language without additional formatting libraries.
  • It’s not easy to create a malicious payload.

But note the disadvantages:

  • This may not be a viable solution if the number of object properties are inconsistent. For example, assume our books had optional PDF URLs or prices in multiple currencies. We may require further delimiters and our parsing will become more difficult.
  • Your web service may be more limited in scope than one which returns XML or JSON. This won’t be an issue if you’re writing services for your own applications but it’s something to consider if you intend publishing the data for third parties.
  • A custom parser must be written for every type of data response.
  • The data is not necessarily human-readable.
  • You must be absolutely certain that delimiter characters do not appear in the original data. You could check for their existence or consider using delimiters toward the start of the ASCII character set.

I hope this series of articles has given you a number of Ajax data formatting options to consider. Have you used any other data formats?

The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account