r/GoogleAppsScript • u/Its_Fabian_H • Nov 29 '20
Question JSON API / DeepL API In Google Scripts
Hi Guys,
I am not a coder, just someone who tries to learn Google Scripts for personal purposes. Apologies, if this is a very stupid question! I access the DeepL API in Google Scripts, which works just fine.
var url consists of my DeepL API code and the text it shall translate (e.g. "Hi, I am Fabian").
var response = UrlFetchApp.fetch(url); var json = response.getContentText(); var data = JSON.parse(json); Logger.log(data);
This returns the following:
{translations=[{detected_source_language=EN, text=Hallo, Ich bin Fabian.}]}
I want the output to be just the translated text ("Hallo, Ich bin Fabian."). I tried to get there by changing Logger.log(data) to Logger.log(data.translations.text), which then unfortunately returns "null". What am I doing wrong?
8
u/BlackDynamite514 Nov 29 '20
Try data.translations[0].text
It looks like translations is an array so you’ll need to get the first index (at index 0), then get the text from there.
I’m on mobile right now so I can test it but give that a shot!