Apps script to fetch KOBO data

Hello my fellow data enthusiasts, I am using Apps Script to fetch data using KOBO tookbox API and set it in my google sheet. I am using the following script to achieve this:

function getData() {
var formId = “XXXXXXXXXXXXXXX”; // This is where I enter form UID
var apiUrl = “KoboToolbox Form Building API” + formId + “/data”;
var apiToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX; // This is where I put my API token/Key
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

var headers = {
“Authorization”: "Token " + apiToken

var response = UrlFetchApp.fetch(apiUrl, { headers: headers });
var data = JSON.parse(response.getContentText());

// Paste data into the Google Sheet
for (var i = 0; i < data.length; i++) {
var rowData = [ ];
// Customized according to the data structure
// Add more fields as needed

I am getting the following error:

                              SyntaxError: Unexpected token '<', "

                              <!DOCTYPE "... is not valid JSON

Seems like the response content is not JSON but HTML. I do not know what to do at this point. Would love some help with this.

Welcome back to the community, @shadsaiddiqui! Maybe this post discussed previously should be helpful but the script would need some updates:

Hello, thanks alot for this. I will go through this and respond with further questions (if any).
In the meantime, i figured out how to get JSON response (for those who are curious):

I just changed the the following line →
" var apiUrl = “KoboToolbox Form Building API” + formId + “/data”;"
" var apiUrl = “KoboToolbox Form Building API” + formId + “/data/?format=json”;"

1 Like