0

Working on API response that gets user data from 10K ft system via API. The response looks like:

"data": [{
    "id": 30000,
    "display_name": "John Doe",
    "email": "[email protected]",
  },

I want to save this data in three different arrays for each response to be used later for looping through user id. I am not sure how to go about it. Below is the code I have worked on so far. I will appreciate any help here. Thanks

    function getUsers() {
      var auth = 'authentication'
      var url = 'https://api.10000ft.com/api/v1/users?' + '&auth=' + auth;
      var options = {
        method: 'get',
        headers: {
          Authorization: 'Bearer ' + auth
        }
      };

            };
           let response = UrlFetchApp.fetch(url, options);
           let json = JSON.parse(response);
               };
    var response = UrlFetchApp.fetch(url, options);
    var json = JSON.parse(response);
    var ids = [];
    var display_names = [];
    var emails = [];

    function result(data) {

    data.forEach(element => {
                 ids.push(element.id);
    display_names.push(element.display_name);
    emails.push(element.email);
  });

  return { "ids": ids, "display-names": display_names, "emails": emails };
}

console.log(result(data));

enter image description here

2 Answers 2

3

From what I understand...Maybe you're looking for something like this?

    let json = JSON.parse(response);
    let ids = [];
    let dis_name = [];
    let email = [];

    for(let i = 0; i < json.length; i++){
        ids.push(json[i].id);
        dis_name.push(json[i].display_name);
        email.push(json[i].email);
    }

P.S Consider using let keyword instead of var.

4
  • hey Nicolas, thanks for looking at my code. You are right and have interpreted my question correctly. I have edited my question with the current code from your recommendation. I am running this in google app script console and after using let as per your suggestion it throws an error Missing ; before statement. Could I be missing something?
    – Just
    Commented Jan 20, 2020 at 9:36
  • 1
    Yeap I've made a mistake at the beginning of the for loop. Just updated my code. Just copy the code again and everything should be fine. Cheers. Commented Jan 20, 2020 at 9:43
  • Thanks Nicolas. Have copied the code, but still have the same error, it comes from the line just before var response = UrlFetchApp.fetch(url, options);. I however changed let to var and the execution was successful. Not sure why I can run it with let
    – Just
    Commented Jan 20, 2020 at 9:53
  • Glad I was able to help mate. Have a nice day ! Commented Jan 20, 2020 at 10:05
1

if "data" contains more objects you need to loop through the data array and push the ids, display_names and emails in separate arrays. The below example will help you.

var data = [
    {
        "id": 30000,
        "display_name": "John Doe",
        "email": "[email protected]"
    },
    {
        "id": 30001,
        "display_name": "John Cena",
        "email": "[email protected]"
    },
    {
        "id": 30002,
        "display_name": "John kabaraya",
        "email": "[email protected]"
    }
]

var ids = [];
var display_names = [];
var emails = [];

function result(data) {

    data.forEach(element => {
        ids.push(element.id);
        display_names.push(element.display_name);
        emails.push(element.email);
    });

    return { "ids": ids, "display-names": display_names, "emails": emails };
}

console.log(result(data));

2
  • Hey, @Sachintha thanks for looking at my code. I have modified my question to incorporated your recommendation but I ran into a syntax error. I have included a screenshot of the same. The error is basically at the data.forEach(element => { Kindly look at it and advice
    – Just
    Commented Jan 20, 2020 at 11:51
  • I'm refering "data" to your response. I advice you to don't copy the given code as it is . you should pass the response to the "result" fumction. Commented Jan 20, 2020 at 12:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.