Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
AppScript API returning empty JSON object

So, I'm trying to build an API using my Google Sheet and I've run into a problem.

Here's my code

function doGet() {
  let doc = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = doc.getSheetByName('portfolios');
  let values = sheet.getDataRange().getValues();

  let output = [];
  for (let i = 2; i < values.length; i++) {
    let row = [];
    row['name'] = values[i][0];
    row['committee'] = values[i][1];
    row['post'] = values[i][2];
    //console.log(row)
    output.push(row);
  }

  console.log(output)

  return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}

When I run the script, I get the desired output in the console but when I deploy the API (I've set "Who has access" to Everyone) and send a GET request, I receive an empty JSON object.

[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]

What am I doing wrong?

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

On behalf of OP, Himanshu Sardana

I was able to fix the error, turns out it was due to me using the JSON.stringify function incorrectly. I was able to fix it by changing:

let row = [];

to

let row = {};

Edited code:

function doGet() {
  let doc = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = doc.getSheetByName('portfolios');
  let values = sheet.getDataRange().getValues();

  let output = [];
  for (let i = 2; i < values.length; i++) {
    let row = {}; // Edit in question
    row['name'] = values[i][0];
    row['committee'] = values[i][1];
    row['post'] = values[i][2];
    //console.log(row)
    output.push(row);
  }

  console.log(output)

  return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda