1

I am stuck. It seems that this should be working, but it is not.

I am trying to access an object's key values, but it is not working. I've done this before and have read plenty of things here on stack overflow and elsewhere, but it still does not work.

Here is my code:


  var validate = UrlFetchApp.fetch(url, options);
  
  Logger.log('validate = ' + validate);
  Logger.log("validate['response'] = " + validate["response"]);
  Logger.log('validate.response = ' + validate.response);
  Logger.log("validate.['done'] = " + validate["done"]);
  Logger.log("validate.done = " + validate.done);
  
  var validateObj = JSON.stringify(JSON.parse(validate));
  
  Logger.log("validateObj = " + validateObj);
  Logger.log("validateObj.response = " + validateObj.response);
  Logger.log("validateObj.done = " + validateObj.done);

and here are the logs:

validate = {
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
    "result": "Invalid Full Name.  Please enter a valid Full Name."
  }
}

validate['response'] = undefined

validate.response = undefined

validate.['done'] = undefined

validate.done = undefined

validateObj = {"done":true,"response":{"@type":"type.googleapis.com/google.apps.script.v1.ExecutionResponse","result":"Invalid Full Name.  Please enter a valid Full Name."}}

validateObj.response = undefined

validateObj.done = undefined

When using bracket notation, I've tried using single and double quotes as well.

Can anyone help me to see the error?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Chris
  • 473
  • 5
  • 18

1 Answers1

3

You are trying to access the response and done property from a string, therefore you are getting undefined. You have tried to do JSON.parse on the response but then JSON.stringify it back to a string, just do JSON.parse and access the property

var validateObj = JSON.parse(validate); // Now you have a deserialized object

Logger.log("validateObj = " + validateObj); // You should get "validateObj = [object Object]"
Logger.log("validateObj.response = " + validateObj.response);
Logger.log("validateObj.done = " + validateObj.done);
Logger.log("validateObj[ 'response' ] = " + validateObj[ 'response' ] );
Logger.log("validateObj[ 'done' ]= " + validateObj[ 'done' ]);
Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24