How to retrieve nested JSON Data?

630 views
Skip to first unread message

Scott Holcomb

unread,
Apr 25, 2019, 10:32:10 AM4/25/19
to Google Apps Script Community

I am attempting to retrieve data from an API with a JSON structure of nested data. I am able to gather the first tier of data and push/pull to a Google Sheets back-end, however I cannot do the same for any objects that are nested deeper. The closest code snippet I've stumbled upon is below but renders the output "undefined". I'm hoping this is just some obvious oversight in the code and any help would be greatly appreciated.

API JSON Data layout Example - 

 {
        "siteName": "XXX",
        "brand": "XXX,
        "timestamp": "2019-04-25 14:20:23",
        "h2sTrafficUtilizationMbps": 33.706497873,
        "s2hTrafficUtilizationMbps": 8.603756497,
        "h2sRequestedMbps": 72.671550363,
        "s2hRequestedMbps": 9.406809277,
        "latitude": "9.96554",
        "longitude": "76.1724",
        "heading": 266.12,
        "speedKnots": 0,
        "antennas": {
            "S Dac 2202 Ku Band": {
                "status": "Error",
                "azimuth": "-234.08",
                "elevation": "-288.34",
                "heading": "-114.51",
                "modems": [
                    "XXX Ku-9252"
                ]
            },
            "I 240M": {
                "status": "Unlocked",
                "azimuth": "98.022",
                "elevation": "29.81",
                "heading": "266.12",
                "modems": [
                    "XXX Intellian 9s C-2344",
                    "XXX Intellian Ku-34894"
                ]
            }
        },
        "modems": [
            {
                "name": "XXX Ku-9252",
                "s2hSnr": "6.3",
                "s2hLock": "In Network",
                "s2hPower": "-5.5",
                "cpuPercentUtilized": "8.4",
                "h2sCirMbps": 15,
                "s2hCirMbps": 5,
                "h2sUtilizationMbps": "0.006776383",
                "s2hUtilizationMbps": "0.004839437",
                "h2sRequestedMbps": "0.006776383",
                "s2hRequestedMbps": "0.004839437",
                "networkName": "APSTAR9_N1_IDRCT_WestV_WestV",
                "plannedPriority": "2",
                "h2sQosAllocated": [
                    {
                        "name": "Total",
                        "min_mbps": "0.005886",
                        "avg_mbps": "0.00677638",
                        "max_mbps": "0.007145"



Code Snippet being used to gather first tier of data successfully - 

  var response = UrlFetchApp.fetch(url, options);
  var data = JSON.parse(response.getContentText());

    if (!data.length) {
        return [];
    }

  var rows = [];
  var ParentHeaders = Object.keys(data[0]);

    rows.push(ParentHeaders);

    for (var i=0; i<data.length; i++) {
        var row = [];
        for (var j=0; j<ParentHeaders.length; j++) {
            row.push(data[i][ParentHeaders[j]]);
        }
        rows.push(row);
    }

//  var sheet = SpreadsheetApp.getActiveSheet();
  var sheet = SpreadsheetApp.getActive().getSheetByName('JSON_RESPONSE')
  var appendRange = sheet.getRange(sheet.getLastRow() + 1,1,rows.length,13);
      
  appendRange.clearContent();
  appendRange.setValues(rows);

 
}

function displayShipData(rows) {
  
  var output = ImportJSON(url);

  rows.forEach(function(elem,i) {
    elem.unshift(i + 1);
  });

  var len = output.length;

  sheet.getRange(15,1,102,12).clearContent();

  sheet.getRange(15,1,102,12).setValues(rows);
}



Code Snippet that should allow for nested item retrieval but pulls in "undefined" V1-

  var response = UrlFetchApp.fetch(url,options); 
  var dataAll = JSON.parse(response.getContentText());
  var dataSet = dataAll.data.modems;
  var rows = [],data;

for (var i in dataSet) {
  data = dataSet[i];
    rows.push([data.siteName, data.brand, data.timestamp, data.latitude, data.longitude, data.name, data.s2hLock]);
 
    
  };
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1')
  var range = sheet.getRange(1,1,100,7);
  range.clearContent();
  range.setValue(rows)


  } 



Code Snippet that should allow for nested item retrieval but pulls in "undefined" V2- 


  var response = UrlFetchApp.fetch(url,options);

  var dataAll = JSON.parse(response.getContentText()); //
  var dataSet = dataAll;
  Logger.log(dataSet)
  var rows = [],
      data;
  Logger.log(rows)

  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.siteName,data.brand,data.modems,[modems.networkName]]); 
  }

  
  var len = rows.length;
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange(1,1,102,13).clearContent();
  var appendRange = sheet.getRange(sheet.getLastRow() + 1,1,len,3);
  
  appendRange.setValues(rows);
}

Reno Blair

unread,
Apr 25, 2019, 10:55:18 AM4/25/19
to google-apps-sc...@googlegroups.com
You've set data to response.modems[i] in v1 and v2. Per your API sample, the modems object does not have the properties siteName, brand, modems, or [modems.networkName].

What is the complete result format you are trying to achieve in this conversion?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ea72ab79-24f5-46d1-a7ba-f2faeed2337c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Reno Blair
Educational Technology Services​

Riël Notermans

unread,
Apr 25, 2019, 3:39:10 PM4/25/19
to Google Apps Script Community
Can you post the whole JSON file?

Riël Notermans

Op woensdagen vrij • Zzapps B.V. • High Tech Campus 9, 5656 AE Eindhoven • T 040 711 41 94 • E ri...@zzapps.nl • www.zzapps.nl
WIJ MAKEN HET ONDENKBARE MOGELIJK


Michael O'Shaughnessy

unread,
Apr 25, 2019, 4:58:20 PM4/25/19
to google-apps-sc...@googlegroups.com

Faustino Rodriguez

unread,
Apr 26, 2019, 3:48:11 PM4/26/19
to Google Apps Script Community
There is a library from Bruce cFlatten that convert any deep JSON data into an Array of JSON flattened into a single level, with keys/headers ready for a Google Sheets update
Check this link for full details, code and samples

p.s. not sure if that's what you need, but I hope it helps, as it does for me
Thanks to @Bruce Mcpherson

Adam Morris

unread,
Apr 26, 2019, 4:15:56 PM4/26/19
to google-apps-sc...@googlegroups.com
I wrote Dotmitizer.gs which does this as well.


————————————————————————————

Adam Morris | IT Systems & English Teacher | IGB International School
Jalan Sierramas Utama, Sierramas,
47000 Sungai Buloh, Selangor DE, Malaysia

t    +60 3 6145 4688
f    +60 3 6145 4600
w   www.igbis.edu.my
e    adam....@igbis.edu.my

————————————————————————————


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
Reply all
Reply to author
Forward
0 new messages