In Google Apps Script, I am connecting to a service via OAuth2. I get a JSON response and parse it with JSON.parse(response.getContentText()); so it logs something like so:
{Results=[{Id=45364743, Description=null, Name=Math I , IsActive=true},
{Id=45364768, Description=null, Name=Math II, IsActive=true}]}
Using the following code, I have previously taken JSON responses and written them successfully to Google Sheets. However, this time the FOR loop does not run because dataSet does not have a length. I am guessing that is because everything is nested under "Results".
function getService2() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Data');
var key = 'kjeu7hjf7873alkjhehjhfayuluoojsds'
var options = {
method: "get",
headers: {
Authorization: "Bearer " + key,
api_key: key,
}
}
var response = UrlFetchApp.fetch(URL, options);
var dataAll = JSON.parse(response.getContentText());
var dataSet = dataAll;
Logger.log(dataSet)
var rows = [],
data;
for (i = 0; i < dataSet.length; i++)
if ( dataSet.length > 0.0){
data = dataSet[i];
rows.push([data.Id, data.Name, data.IsActive ]); //your JSON entities here
dataRange = sheet.getRange(lastRow + 1, 1, rows.length , 2);
dataRange.setValues(rows);
}}
How would I determine the length of the parsed JSON response (i.e. "dataSet") to run the loop and write to the sheet?
var dataSet = dataAll;withvar dataSet = dataAll.Results;