You want to use jsonData
at other functions. I think that there are 3 patterns for your situation.
Pattern 1
This is a simple solution. You redeclare jsonData
in doPost(e)
. By this, undefined
occurs. So modify as follows. In this case, jsonData
is cleared after the running script was finished.
From :
var jsonString = e.postData.getDataAsString();
To :
jsonString = e.postData.getDataAsString(); // Remove "var"
Pattern 2
This is a solution that even if the running script is finished and it is run again, jsonData
can be used. But there is a time limit. In this case, it uses Cache Service. When Cache Service is used, the value is required to be the string data.
function doPost(e){
try{
var jsonString = e.postData.getDataAsString();
setLog("***json String = " + jsonString + " ***");
// jsonData = JSON.parse(jsonString);
var cache = CacheService.getScriptCache();
cache.put('jsonString', jsonString, 1500); // cache for 25 minutes
}
catch(e){
setLog("***Exception occured = "+JSON.stringify(e) + " ***");
}
}
// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
var cache = CacheService.getScriptCache();
var jsonString = cache.get('jsonString');
var jsonData = JSON.parse(jsonString);
// do something
}
Pattern 3
This is a solution that even if the running script is finished and it is run again, jsonData
can be used. In this case, it uses Properties Service. When Properties Service is used, the value is required to be the string data and there is no time limit.
function doPost(e){
try{
var jsonString = e.postData.getDataAsString();
setLog("***json String = " + jsonString + " ***");
// jsonData = JSON.parse(jsonString);
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('jsonString', jsonString); // Save jsonString to the property.
}
catch(e){
setLog("***Exception occured = "+JSON.stringify(e) + " ***");
}
}
// When you use jsonData, please JSON.parse() like this function.
function myFunction() {
var scriptProperties = PropertiesService.getScriptProperties();
var jsonString = scriptProperties.getProperty('jsonString');
var jsonData = JSON.parse(jsonString);
// do something
}
If I misunderstand your situation, I'm sorry.