skip to Main Content

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:

enter image description here

enter image description here

2

Answers


  1. It is because the value you need to return is on a new line after the return:

    return
    [user["department_text"]]
    

    Once you put them on the same line, it should return proper value.

    Login or Signup to reply.
  2. This

    return
    [user["department_text"]]

    Must be a one line:

    return [user["department_text"]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search