Return JSON with ContentService

September 17, 2017Web App, Google Apps Scriptjson, contentservice

Javascript Object Notation is one of the most common formats for sending data over the internet.

If you are building any sort of application for the web it is hard to believe that you wouldn’t have come across JSON somewhere along the way.

You actually can use a deployed web app with Google Apps Script to serve JSON data to other services on the web. It is like a simple backend server without any of the hosting fees!

In the documentation they give this example:

function doGet(request) {
  var events = CalendarApp.getEvents(
    new Date(Number(request.parameters.start) * 1000),
    new Date(Number(request.parameters.end) * 1000)
  );
  var result = {
    available: events.length == 0,
  };
  return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(
    ContentService.MimeType.JSON
  );
}

In my video I do a little more of a deep dive into how to use the ContentService to serve your data. If you run into CORS problems you can also use JSONP.

Want to learn more about this? Send me a message on twitter or leave a comment below!

jordan rhea

Jordan Rhea - I’m a front-end web developer. Follow me at @rheajt if you want to talk, discuss, or fix any of my code! I am passionate about using JavaScript in Google projects, but there are many more applications. I want to learn them all.