0

I've been trying to get a Google App Script together that works with Google's Directory API to update a large number of groups.

To my knowledge Google App Scripts uses javascript. I can't find a library to import or a json.stringify function to copy/paste into my code. Does anyone know of one?

I have the following code after the necessary authentication steps.

 var options = {
   'method' : 'Post',
   'contentType' : 'application/json',
   'followRedirects' : true,
   'muteHttpExceptions': true,
   'headers': {Authorization: 'Bearer ' + service.getAccessToken()},        
   'payload' : JSON.stringify(data)
 };


  var url = "https://www.googleapis.com/admin/directory/v1/groups/" + groupkey + "/members";
    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText());
}

Error Message:

TypeError: Cannot find function stringify in object [object Object]. (line 219, file "Code")

2
  • You need to provide more details. In testing, I can stringify without any additional imports or similar. function myFunction() { Logger.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))); // '"2006-01-02T15:04:05.000Z"' Logger.log(JSON.stringify({ x: 5, y: 6 })); // '{"x":5,"y":6}' Logger.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)])); // '[3,"false",false]' } Commented May 1, 2018 at 19:15
  • What's the structure of data? Commented May 1, 2018 at 19:26

1 Answer 1

3

JSON.stringify() should "just work", there is no "import" or similar action required.

Based on your error message it looks like you have accidentally assigned an object to the identifier JSON elsewhere in your code, with something like JSON = {};

Search for all instances of "JSON" in your code and see if you are incorrectly assigning it a value somewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I can't believe it was that simple.
I got the same error too and I found out that there is a JSON variable defined in my code. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.