Reading JSON in GAS App Script doPost(e)

Hi everyone,

I am trying to parse the response of an AppSheet bot in my GAS web hook on POST, however, I am not getting much of luck. 

I am sending the following JSON to my GAS webhook on POST, it gives me the correct Task ID.

{
"Task": [
<<Start: SELECT(Task[Task ID], [Task ID] = [_THISROW].[TASK ID] )>>
{
"Command": "Task.Assign",
"TaskID": "<<[Task ID]>>"
},
<<End>>
]
}

my GAS code is as follows

function doPost(e) {

  var returnMessage = cNO_TEXT ;

  var strMessage = "Start execution at " + Utilities.formatDate(new Date(), "GMT+8""dd/MM/yyyy HH:MM:SS");

  var data = JSON.parse(e.postData.contents)["Task"] ;
// do some internal switching and call the appropriate function
}


e.postData.contents is giving me the following JSON output, which is correct. I have validated with an online JSON formatter.
{"Task":{"Command":"Task.Assign","TaskID":"3"}}
However, I am stuck now. I simply not able to access this JSON any further. I have tried to stringify it and parse it back but no luck. I have tried the code in red above but it just gives me a "{".
 
I have also tried to do e.parameters.Task and other variations but e.postData.contents is the only one that has given me the above JSON.
 
Any idea how can I process this JSON in Apps Script so I could access the Command and Task ID along with their values in some way?
 
EDIT
I stumbled upon this Stack Overflow article, but no luck.

Thanks
Kaz
0 27 20.1K
27 REPLIES 27
Top Labels in this Space