jordan rhea header

Return JSON with ContentService

September 17th, 2017

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 wearing a hoodie

Written by jordan rhea a developer focused on building solutions in the cloud

You should follow me on Twitter, or Github, or even on LinkedIn

© 2022 jordan rhea