4

I imported json data into google scripts with:

var doc = Utilities.jsonParse(txt);

I can access most of the objects like such...

var date = doc.data1.dateTime;
var playerName = doc.data1.playerName;
var playerId = doc.data1.playerID;
var teamNumber = doc.data2.personal.team;

A bunch of objects I need to access have numbers as object names...

doc.data2.personal.team.87397394.otherdata
doc.data2.personal.team.87397395.otherdata
doc.data2.personal.team.87397396.otherdata
doc.data2.personal.team.87397397.otherdata

...but when I try to read the data with...

var teamId = doc.data2.personal.team.87397394;

... I get an error "Missing ; before statement."

I tried this...

var teamId = doc.data2.personal.team[87397394];

... and get "teamId undefined" in the log.

I also tied this with the same result...

var teamId = doc.data2.personal.team[+'6803761'];

I can read in the names as strings very easily with "For In", but can't get to the objects themselves. Every example I've found so far uses the brackets so I'm stumped what to try next.

Thank you! Brian

UPDATE

I used this per your suggestions to get the object name into a variable and using the variable in brackets. No error but var test remains "undefined"...

for(var propertyName in doc.data2.personal.team) {
    // propertyName is what you want
    // you can get the value like this: myObject[propertyName]
    Logger.log (propertyNames);
    var test = doc.data2.personal.team[propertyName];
}

The log shows the object names, as expected...
87397394
87397395
87397396
87397397

I'm thinking it's a bug in Google's implementation. Here is an example if anyone wants to verify it. test will return 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];
   }
}
  • If the object name is a number, then doc.data2.personal.team[87397394] should work. Are you sure team has an element object that has the name 87397394... Can you may be print doc.data2.personal.team and check – kameswarib Mar 17 '14 at 07:32
  • The id's do exist... I'm copy/pasting the id's directly from the debug browser built into the google script editor. If I try this... var teamId = doc.data2.personal.team[87397394].data1; I get this error... TypeError: Cannot read property "data1" from undefined, if that helps. I can browse to data1 and see the value. – Brian Hershey Mar 17 '14 at 07:40
  • Here is a partial dump of doc.data2.personal.team... {87397394={damaged=0, data1=0, achievements=[], damageAssistedTrack=0}, 87397395={damaged=2, data1=0, achievements=[], damageAssistedTrack=0}, etc... – Brian Hershey Mar 17 '14 at 07:53
  • `var teamId = doc.data2.personal.team['6803761'];` did you try this ? with the numbers as string ? – Gaurang Tandon Mar 17 '14 at 07:57
  • 3
    If `doc.data2.personal.team[87397394].data1;` gives you that error, then it means that `doc.data2.personal.team` doesn't have a property with name `87397394`. Not much we can do about it. – Felix Kling Mar 17 '14 at 08:13
  • @GaurangTandon: That doesn't make a difference. All property names are implicitly converted to strings anyway. – Felix Kling Mar 17 '14 at 08:13
  • *" can read in the names as strings very easily with "For In", but can't get to the objects themselves."* See http://stackoverflow.com/q/85992/218196 – Felix Kling Mar 17 '14 at 08:14
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Mar 17 '14 at 08:14
  • @FelixKling Interesting. Thanks for letting me know! – Gaurang Tandon Mar 17 '14 at 08:16
  • Can you create a http://jsfiddle.net?, show us what the `txt` variable contains. – Ruan Mendes Mar 17 '14 at 16:54
  • @JuanMendes, I posted the code in the post that anyone can try in Google script editor, thanks! – Brian Hershey Mar 17 '14 at 17:25
  • 1
    Works for me. [Here's a fiddle.](http://jsfiddle.net/antisanity/jUqv2/) – canon Mar 17 '14 at 17:31
  • @canon [It doesn't work here](https://script.google.com/d/15BrPmCbd2SeFqICJ4l6UAVtgcZp0ueePT1Hbg6rSvNHMKCzlg4Pk1uAy/edit?usp=sharing) Brian, this is what your question should contain so that people can easily run your code. I had never even heard of google scripts. Press ctrl+enter to see the output – Ruan Mendes Mar 17 '14 at 18:11

1 Answers1

1

The problem seems to be in the Utitlies.jsonParse. The following works fine

var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = JSON.parse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
    var vehicle = doc.datablock_battle_result.vehicles[propertyName];
    Logger.log('Vehicle id is ' + propertyName);
    Logger.log('Vehicle value is  ' + JSON.stringify(vehicle));
    break;
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • This is exactly correct... Google's JSON.parse works fine, Utilities.jsonParse does not. Thank you all! Now I need some sleep, I was up all night trying to get this to work :) – Brian Hershey Mar 17 '14 at 18:45