Search code examples
jsongoogle-apps-scriptgoogle-directory-api

Getting values from JSON using appscript


I'm creating two-dimentional array with google appscript using chrome devices users with the JSON response from a directory. I've been able to get values. However, when I try to get recentUsers represented as:

    {
    "kind": "directory#chromeosdevices",
    "chromeosdevices": [
     {
       "kind": "directory#chromeosdevice",
       "etag": "1234567890"
       "deviceId": "def456",
       "serialNumber": "234567",
       "status": "ACTIVE",
       "lastSync": "2013-03-05T17:30:04.325Z",
       "supportEndDate": "2014-04-05T17:30:04.325Z",
       "annotatedUser": "help desk",
       "annotatedLocation": "Mountain View help desk Chromebook",
       "annotatedAssetId": "1234567890",
       "notes": "Loaned from support",
       "orderNumber": "1234",
       "willAutoRenew": true,
       "osVersion": "Browser Version 18.0",
       "platformVersion": "Platform Version 1415.2.0",
       "firmwareVersion": "Firmware Version 1.2.3.4",
       "bootMode": "validated",
       "lastEnrollmentTime": "2012-04-05T17:30:04.325Z",
       "orgUnitPath": "corp/engineering",
       "recentUsers": [
         {
           "type": "USER_TYPE_MANAGED",
           "email": "user@customer.com" //I'm trying to get the most recent user's email 
         }
       ],
       "activeTimeRanges": [
         {
           "date": "2012-04-05",
           "activeTime": "3600000"
         }
       ],
     }
  ],
  "nextPageToken": "abcdefghijkl123"
}

I get an array but I can't get the value. I'm trying to get the most recent user's email. Anyone have experience with appscript? I've tried recentUsers[0].email and recentUser[0]['email']. This is the code I have to far: //I commented where I'm trying to push a value enter code here

var chromeArgs = {   
  maxResults: 200,
  orderBy: 'annotatedUser',
  projection: "FULL"
 
};


var getChromes = (AdminDirectory.Chromeosdevices.list('XXXXXXXXX', chromeArgs));

var chromes = getChromes.chromeosdevices;
   if (chromes && chromes.length > 0) {
 Logger.log('Devices:');
 //2d array
 var wholeValues = [];

     for (var i = 0; i < chromes.length; i++){  
//create a 1D array first with pushing 0,1,2 elements with a for loop
   var value = [];
   for (var j = 0; j < 1; j++) {
     var chrms = chromes[i];
     var recentUser = chrms.recentUsers;
     value.push(recentUser[0].email); // Here is where I need help 
}
    //pushing the value array with [0,1,2] to thw wholeValues array. 
    wholeValues.push(value);
  } // the outer for loop runs five times , so five the 0,1,2 with be pushed in to thewholevalues array by creating wholeValues[0][0],wholeValues[0][1]...till..wholeValues[4][2]

  Logger.log(wholeValues); '''

Solution

  • You are very close, the only thing you need to change is to replace the static recentUsers[0] with dynamic recentUsers[j] within the loop

    Sample:

    function getChromes(){
      var chromeArgs = {   
        maxResults: 200,
        orderBy: 'annotatedUser',
        projection: "FULL"  
      };   
      var getChromes = (AdminDirectory.Chromeosdevices.list('XXXXXXXXX', chromeArgs));  
      var chromes = getChromes.chromeosdevices;
      if (chromes && chromes.length > 0) {
        Logger.log('Devices:');
        var wholeValues = [];    
        for (var i = 0; i < chromes.length; i++){  
          var chrms = chromes[i];
          var recentUser = chrms.recentUsers;
          var value = [];
          for (var j = 0; j < recentUser.length; j++) {
            Logger.log(recentUser[j].email);
            value.push(recentUser[j].email); 
          } 
          wholeValues.push(value);
        }     
        Logger.log(wholeValues); 
      }
    }
    

    Note that you need to specify the iteration limit of the inner loop dynamically j < recentUser.length; instead of j < 1 to account for different quantity of recent users for each chrome device.