GAS treating nested object as string

100 views
Skip to first unread message

Efficient Small Business

unread,
Sep 5, 2019, 3:07:42 PM9/5/19
to Google Apps Script Community
Hi there,

I am using UrlFetchApp to send a stringified JSON object to another script.

The doPost(e) on my receiving script returns:
"{parameter={value={schedule=true, hiring=false, dash=true, token=1a2b}, key=1234}, contextPath=, contentLength=137, queryString=, parameters={value=[{schedule=true, hiring=false, dash=true, token=1a2b}], key=[1234]}, postData=FileUpload}"

e.parameters.key and e.parameters.value return the respective values "1234" and "{schedule=true, hiring=false, dash=true, token=1a2b}, key=1234}"

When I attempt to return anything within e.parameter.value, I get "undefined". However, e.parameter.value[0] will return the characters within ".value" which tells me that the nested object within is being treated like a string. 

Why does this happen and is there a work around (other than passing each nested variable as its own url parameter)?

Thanks,
Micah

AD:AM

unread,
Sep 5, 2019, 7:49:32 PM9/5/19
to Google Apps Script Community
Hi Micah

On Friday, 6 September 2019 05:07:42 UTC+10, Efficient Small Business wrote:
However, e.parameter.value[0] will return the characters within ".value" which tells me that the nested object within is being treated like a string.

So what does, say, e.parameter.value[0].schedule return?

Cheers
Adam 

Efficient Small Business

unread,
Sep 6, 2019, 1:49:36 AM9/6/19
to Google Apps Script Community
Hey Adam,

"e.parameter.value[0].schedule" returns undefined. 
  • e.parameter.value[0] returns "{"
  • e.parameter.value[1] returns "s"
  • e.parameter.value[2] returns "c"
  • etc...

Václav Petrák

unread,
Sep 6, 2019, 2:05:48 AM9/6/19
to google-apps-sc...@googlegroups.com
Try:
var json = JSON.parse(e.parameter.value);
var shedule = json.schedule;
Václav Petrák

 
 

e-mail: waclav...@gmail.com


pá 6. 9. 2019 v 7:49 odesílatel Efficient Small Business <efficient.sm...@gmail.com> napsal:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/7618af61-9feb-4f6c-86ab-bcb6fa7d64d8%40googlegroups.com.

Efficient Small Business

unread,
Sep 12, 2019, 1:55:01 PM9/12/19
to Google Apps Script Community
Thanks for the suggestion Václav Petrák

Unfortunately, that still returns undefined.


On Thursday, September 5, 2019 at 11:05:48 PM UTC-7, waclav.petrak wrote:
Try:
var json = JSON.parse(e.parameter.value);
var shedule = json.schedule;
Václav Petrák

 
 

e-mail: waclav...@gmail.com


pá 6. 9. 2019 v 7:49 odesílatel Efficient Small Business <efficient.s...@gmail.com> napsal:
Hey Adam,

"e.parameter.value[0].schedule" returns undefined. 
  • e.parameter.value[0] returns "{"
  • e.parameter.value[1] returns "s"
  • e.parameter.value[2] returns "c"
  • etc...


On Thursday, September 5, 2019 at 4:49:32 PM UTC-7, AD:AM wrote:
Hi Micah

On Friday, 6 September 2019 05:07:42 UTC+10, Efficient Small Business wrote:
However, e.parameter.value[0] will return the characters within ".value" which tells me that the nested object within is being treated like a string.

So what does, say, e.parameter.value[0].schedule return?

Cheers
Adam 

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Efficient Small Business

unread,
Sep 12, 2019, 2:24:51 PM9/12/19
to Google Apps Script Community
I found a work around, but I still don't understand why this is happening.

Work around:
If I encrypt the object before sending the POST request, then decrypt on the receiving script, I am able to access nested object keys as expected.

Alan Wells

unread,
Sep 12, 2019, 2:26:17 PM9/12/19
to Google Apps Script Community
I'm wondering if the payload being sent to your receiving script is sending a stringified object that is NOT JSON.  And the consequence being that trying to parse it doesn't work.
I'd make sure that whatever the sending script is sending is valid JSON.  Apps Script is not written in JavaScript.  And the objects that it returns is not JSON.  You can obviously still get elements and values out of returned object from Apps Script just as you would with JSON, but it may need to be converted to JSON if you want to get the entire object.
The object that you posted with the equal signs is not valid JSON.  I've often seen that in the logs, if log out the return value.
What is the code that creates and sends the payload?
Reply all
Reply to author
Forward
0 new messages