I’m trying to fetch JSON data from my API to import to Google Sheets using Google Apps Script. Everything is working as expected, except for when I’m trying to map the object, the result array returns undefined objects.
Here’s my script:
function GETMYJSONDATA() {
var options = {
"method" : "get",
"headers" : {
"Authorization": "Bearer REDACTED"
}
};
const url = "REDACTED"
const res = UrlFetchApp.fetch(url, options)
const dataAsText = res.getContentText()
const data = JSON.parse(dataAsText)
const results = data.response.users.map (user => {
return
[user["department_text"]]
})
return results
}
Below is the JSON response from my API (raw) and the Apps Script debugger result:
2
Answers
It is because the value you need to return is on a new line after the
return
:Once you put them on the same line, it should return proper value.
This
Must be a one line: