Fetch not out-putting all data to Google sheet

Viewed 102

I am using Fetch to out-put JSON data to google sheets

The Fetch function is not outputting all the data in the JSON to the sheet

Here is what I get when I run my Fetch function enter image description here

But here is what it should be if it were adding all the data in the JSON enter image description here

You can see that columns N through R are not being added to the sheet

Here is my Fetch function

function fetchData() {
  // Set Fetch Variables
  var url = "https://sum-app.net/projects/13435720200118544/download_data/combined_json"
  var response     = UrlFetchApp.fetch(url);
  var responseText = response.getContentText();
  var responseJson = JSON.parse(responseText); 

  var connectionKeys = Object.keys(responseJson.connections[0]); 
  var data = responseJson.connections.map(e => connectionKeys.map(f => {
      return e[f] instanceof Array ? e[f].join("|") : e[f];
  })); 
  data.unshift(connectionKeys);
  data = data.map(x => x.map(y => typeof y === 'undefined' || null ? "" : y));

  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  sh.clearContents();//clear sheet
  sh.getRange(1,1,data.length,data[0].length).setValues(data);//load the data
}

Here is the JSON: https://sum-app.net/projects/13435720200118544/download_data/combined_json

If it is helpful here is a Google sheet with the Fetch function https://docs.google.com/spreadsheets/d/1CuP2DDO-rB1henrMAurBgb-CYmh7RNBbSHOjF6BqG5c/edit?usp=sharing

Thank you in advance for any assistance with getting the fetchData function to output all the data in the JSON

1 Answers

What I suggest is to replace

var connectionKeys = Object.keys(responseJson.connections[0]); 

by

  let myDico = new Map()
  responseJson.connections.forEach(function(key){
    Object.keys(key).forEach(function(item){
       myDico.set(item,'')
    })
  })
  var connectionKeys = []
  myDico.forEach(function(value, key) {
    connectionKeys.push(key)
  })
Related