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
rowData.push(data[i].field1);
rowData.push(data[i].field2);
// Add more fields as needed
sheet.appendRow(rowData);
}
}
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.