The code below generates an output and I am trying to push some of this data into an array. The log shows data, but the iteration doesn't seem to work.
const API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
function getcandidates() {
const url = 'https://api.catsone.com/v3/candidates';
const params = {
'muteHttpExceptions': true,
'method': 'GET',
'redirect': 'follow',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Token ' API_KEY
}
};
const response = UrlFetchApp.fetch(url, params);
const getText = response.getContentText();
const dataSet = JSON.parse(getText);
Logger.log(dataSet);
let rows = [],
data;
for (let i = 0; i < dataSet.length; i ) {
data = dataSet[i];
Logger.log(data) //Nothing gets logged here
rows.push([data.desired_pay, data.contact_id, data.date_created, data.is_registered]);
}
}
Here's the output of the first log, with highlighted "fields" I'd like to get:
CodePudding user response:
When I saw your sample image, it seems that dataSet
is like {_embedded: {candidates: [,,,]}}
. The values of desired_pay
, contact_id
, date_created
, is_registered
you want to retrieve are in the array. So how about the following modification?
From:
const dataSet = JSON.parse(getText);
To:
const dataSet = JSON.parse(getText)["_embedded"]["candidates"];
- From your sample image, I couldn't correctly confirm whether the key for the array is
_embedded
andcandidates
. So please check it again.