0

I am making a Google Sheet that gets info from an API. In Google Script, I want to send the text part of the response only to my cell.

function myFunctionPost() {
  
  var ss = SpreadsheetApp.getActiveSheet(); 
  var input = ss.getRange(2,2).getValue();
  
  var url = "https://www.whatever.com/example/api";
  var data = {
    "prompt": input
  };
  var payload = JSON.stringify(data);

  var headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": "Bearer MY_API_KEY"
  };

  var options = {
    "method": "POST",
    "headers": headers,
    "payload": payload
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
  
  var output = response.getContentText();
  
  ss.getRange(3,2).setValue([output]);
  
}

Current output:

{"text":"good text and yes","second":true,"third":"bad text and no"}

Desired output:

good text and yes

How to I select only the text? I could extract it using formulas, but I rather do it cleanly in the script.

mawns
  • 33
  • 4
  • 1
    too late for answer ... `ss.getRange(3,2).setValue(JSON.parse(output).text))` – Mike Steelson Jan 01 '22 at 12:01
  • @MikeSteelson Thank you so much. What I ended up doing was `var text = JSON.parse(response).text; ss.getRange(3,2).setValue([text]);` – mawns Jan 01 '22 at 17:22

0 Answers0