Currently, I am retrieving JSON data from an API source and preparing to store it in an array within Google Sheets. To format the data correctly, I have utilized JSON.parse
, resulting in the following structure:
[{EmployeeRef={name=value, value=value}, NameOf=value, Hours=value, TxnDate=value, HourlyRate=value, Minutes=value, PayrollItemRef=value, SyncToken=value, BillableStatus=value, sparse=value, MetaData={CreateTime=value, LastUpdatedTime=value}, domain=value, ItemRef={name=value, value=value}, ID=value, CustomerRef={name=value, value=value}, Taxable=value}, etc.]
However, I encounter an error when attempting to push this formatted data into the designated empty array. Below is the code snippet being used for this purpose:
results.forEach(function(elem,z) {
output.push([elem["TxnDate"], elem["EmployeeRef"]['name'], elem["CustomerRef"]['name'], elem["ItemRef"]['name'], elem["BillableStatus"], elem["HourlyRate"], elem["Hours"], elem["Minutes"], elem["Description"]]);
});
The specific error message received is as follows:
"TypeError: Cannot read property "name" from undefined. (line 62, file "Code")".
To investigate further, I attempted to extract a set of values from the array with the following line of code:
Logger.log(results[0]['EmployeeRef']['name']);
This resulted in:
[18-12-06 13:27:41:465 EST] value
[18-12-06 13:27:41:878 EST] value
What is puzzling is why two values are returned in this instance. I initially expected results[0] to return one 'row' from the array, but it appears to be a combination of 'EmployeeRef' and 'name' values from both results[0] and results[1].
Could this issue be the cause of the TypeError message? Any insights on what might be going wrong here?