After importing JSON data into Google Scripts, I am able to access various objects using the code snippet below:
var doc = Utilities.jsonParse(txt);
For most objects, I can easily retrieve specific properties like this...
var date = doc.data1.dateTime;
var playerName = doc.data1.playerName;
var playerId = doc.data1.playerID;
var teamNumber = doc.data2.personal.team;
However, when dealing with objects named with numbers, such as...
doc.data2.personal.team.87397394.otherdata
doc.data2.personal.team.87397395.otherdata
doc.data2.personal.team.87397396.otherdata
doc.data2.personal.team.87397397.otherdata
... I encounter an error message "Missing ; before statement" while trying to read the data with...
var teamId = doc.data2.personal.team.87397394;
I attempted accessing these objects using brackets, but received an "undefined" value in the log...
var teamId = doc.data2.personal.team[87397394];
I also tried converting the number to a string, but had no success...
var teamId = doc.data2.personal.team[+'6803761'];
Although I can read object names as strings using "For In", I'm struggling to access the objects themselves. Any help would be appreciated! Thank you, Brian.
UPDATE
Following suggestions, I attempted to store object names in a variable and use it within brackets. However, the test variable still remains "undefined"...
for(var propertyName in doc.data2.personal.team) {
// propertyName refers to the object name
// Value can be accessed like this: myObject[propertyName]
Logger.log (propertyNames);
var test = doc.data2.personal.team[propertyName];
}
The log does display object names successfully...
87397394
87397395
87397396
87397397
This issue might be related to a bug in Google's implementation. You can verify it by using the following example. The value of test will remain undefined...
function myFunction1() {
var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = Utilities.jsonParse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
Logger.log(propertyName);
var test = doc.datablock_battle_result.vehicles[propertyName];
}
}