'Number of columns in the data does not match the number of columns

I have a problem with the script below. If I download date from .json with lots of data in child I get error: Exception: The number of columns in the data does not match the number of columns in the range. The data has 2 but the range has 1. For example: {"Parent": {"Child": {"1": "TRUE", "2": "FALSE"}, "Child2": {"1": "FALSE"}}} If there is only one type, everything works fine. for example: {"Parent": {"Child": {"1": "TRUE"}, "Child2": {"1": "FALSE"}}}. The error persists when manually increasing the number of columns

function chunkArray(myArray, chunk_size){
    var index = 0;
    var arrayLength = myArray.length;
    var tempArray = [];
    
    for (index = 0; index < arrayLength; index += chunk_size) {
        myChunk = myArray.slice(index, index+chunk_size);
        // Do something if you want with the group
        tempArray.push(myChunk);
    }

    return tempArray;
}

function flatten(arrayOfArrays){
  return [].concat.apply([], arrayOfArrays);
}

function insertJSON(){
  var aOneName = "id";
  var sheet = SpreadsheetApp.getActiveSheet();
  var fileURL = "link to .json"
  var res = UrlFetchApp.fetch(fileURL,{'muteHttpExceptions': true});
  var content = res.getContentText();
  var json = JSON.parse(content);
  var rows = [Object.keys(json)]; 
  var toRows = rows[0].length;
  var toRows = toRows + 1;
  Logger.log("A2:A" + toRows);
  Logger.log(rows);
  var rowsflate = flatten(rows);
  var rowstocols  = chunkArray(rowsflate, 1);
  Logger.log(rowstocols);
  sheet.getRange("A2:A" + toRows).setValues(rowstocols); 
  sheet.getRange(1, 1).setValue(aOneName); 
  
  var headers = json[rows[0][2]];
  Logger.log(headers);
  var getHeaders = [Object.keys(headers)];
  Logger.log(getHeaders);
  sheet.getRange(1,4,getHeaders.length,getHeaders[0].length).setValues(getHeaders); 
  content
  var content = [];
  var temp = [];
  for (var i = 0; i < rows[0].length; i++) {
  var temp2 = [Object.values(json[rows[0][i]])];// Retrieve values to be split.
  content.push(flatten(temp2));
  }
  Logger.log(content);
  var columncount1= rows[0].length;
  Logger.log(columncount1);
  var columncount2 = getHeaders[0].length;
  Logger.log(columncount2);
  sheet.getRange(1,1,columncount1,columncount2).setValues(content); 
}


  [1]: https://i.stack.imgur.com/0UkjR.png
  [2]: https://i.stack.imgur.com/wjqnh.png


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source