Google Apps Script JSON properties with null value

1,520 views
Skip to first unread message

Admin Oregon Gutter

unread,
Oct 26, 2021, 4:18:24 AM10/26/21
to Google Apps Script Community
Currently, when Google Apps Script returns a JSON object with a null value to Javascript, the property is dropped from the return. Those null values are important for my checks because they are null values from database checks.
1) Is this intended?
2) How do I get it to return null values?

Sample Google Apps Script:
function returnTest() {
  var returnData = {
    fakeNumber:1234,
    nullValue:null,
    undefinedValue:undefined
    }
    return returnData;
}

Javascript log of returnData: 
{  fakeNumber:1234 }

dimud...@gmail.com

unread,
Oct 26, 2021, 9:36:52 AM10/26/21
to Google Apps Script Community
Tested your function (executed directly in the IDE) using `console.log()` and got back the expected value:

{ fakeNumber: 1234, nullValue: null, undefinedValue: undefined }

Are you performing some transformation on the returnData object before logging it?

Admin Oregon Gutter

unread,
Oct 26, 2021, 11:00:38 AM10/26/21
to Google Apps Script Community
It works when called in Google Apps Script. The problem is when it is passed back to Javascript. 

Google Log: { FakeNumber:1234,  nullValue:null, undefinedValue:undefined }
JavaScript Log: { FakeNumber:1234 } 

Expected JavaScript: { FakeNumber:1234,  nullValue:null, undefinedValue:undefined }

Javascript functions:
function getResults() {
  google.script.run.withSuccessHandler(consoleLogResults).withFailureHandler(consoleLogResults).returnTest();
}
function consoleLogResults(returnData) {
  console.log(returnData);
}

dimud...@gmail.com

unread,
Oct 26, 2021, 11:59:07 AM10/26/21
to Google Apps Script Community
Ohh...so you're passing an object from Apps Script to client-side Javascript.
 
You can try stringifying the return data and then parsing it on the client-side. You'll still lose the undefined keys (JSON.stringify() strips them out, undefined is not supported in JSON) but the nullValue key should pass through:

// Apps Script
function returnTest() {
    return JSON.stringify({
        fakeNumber: 1234,
        nullValue: null,
        undefinedValue: undefined 
    });
}
.
.
.
// Client-side Javascript
function consoleLogResults(returnData) {
    console.log(JSON.parse(returnData));

Admin Oregon Gutter

unread,
Oct 26, 2021, 12:17:51 PM10/26/21
to Google Apps Script Community
Yes, I am passing an object back to JavaScript. 

I used to get null values in my return and it seems like it has changed in the last week.

I was wondering if this was an intended change and how I can fix it without sifting through all of my code. Also, if it is an intended change, where can I find this info?

Any thoughts?

Stanislav Tvarůžek

unread,
Dec 5, 2021, 6:56:25 AM12/5/21
to Google Apps Script Community
The same issue here. After spending some time figuring out why my add-on stopped working as there was no change done, yap, null properties are removed from response... 

I raised an issue, please mark it / comment there:
Reply all
Reply to author
Forward
0 new messages