0

I am learning how to make API requests using Javascript.

I have made a GET request and got the following data and I would like to loop through them to get only id, display_name and email. This are to be stored in arrays. How do I complete the javascript code?

"data": [
        {
            "id": 222226,
            "display_name": "jane Doe",
            "email": "janeDoe@testmail.com",
        },
        {
            "id": 1111226,
            "display_name": "james",
            "email": "james@testmail.com",
        },
    {
            "id": 11126,
            "display_name": "Travis Mc",
            "email": "travis@testmail.com",
         },

Heres is my javascript code.

var options = {
        method: 'get',
        headers: {
            Authorization: 'Bearer ' + auth
        }
    };
    var response= UrlFetchApp.fetch(url, options);
    var data = JSON.parse(response.getContentText());

I am not sure I am doing this loop part right.

for (var i = 0; i < data.length; i++) {
    Logger.log(data[i].display_name);
}

enter image description here

Just
  • 437
  • 5
  • 15
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – zero298 Jan 20 '20 at 18:44
  • @zero298 I have tried to run this in google app script console but I keep running into a syntax error. check the screenshot in my edited question. Specifically, line 15 – Just Jan 21 '20 at 05:11

1 Answers1

1

I am going to assume you are using the Fetch Api. If so this means you are creating a Promise. When working with promises you will have to chain a then function to establish how to proceed. Promise.then((value)=>{ /* do something */ }); Since you are working with JSON data you must first parse it before it can become manipulatable for javascript. If you read the Fetch Api it explains how it resolves your promise and returns a Response based on the HTTP Response.

This exampled is borrowed from the Devloper Mozilla Org Check out the usage section of the Fetch API

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });

This examples illustrates how the fetch function resolves the promise that returns a response which is parsed then returned again to another then function which will now have the parsed data ready to manipulate.

In your case the code will look something like this

var options = {
    method: 'get',
    headers: {
        Authorization: 'Bearer ' + auth
    }
};

var response = UrlFetchApp.fetch(url, options)
.then((r)=>{ return r.json(); }).then((data)=>{
  for (let i = 0; i < data.length; i++) {
    Logger.log(data[i].display_name);
  }
});
Cesar Perez
  • 109
  • 6
  • Hey, @Cesar thanks for looking at this. I have tried your code but I get a syntax error. I have attached a screenshot to my edited question. Please have a look at line 15. – Just Jan 21 '20 at 05:01
  • @levi It appears to be fine in syntax . I have tried running this on the console and works fine. Although I have some questions . where are you declaring the Object `UrlFetchApp` and the `url` variable . also another way you can try to chain the **then** function is the following : `response.then((r)=>{ return r.json(); }).then((data)=>{ console.log(data); }); `. Let me know if this helps. – Cesar Perez Jan 21 '20 at 17:37