Hello community!
@Tony_KRTand I are looking for a way to retrieve data from a JSON file accessed from an API with Basic HTTP Encryption.
This is the structure of the JSON file:
{
- "results": [
+ {...},
+ {...}....
],
- "next_page_url": [url for the next page]
}
The "next_page_url" contains an URL in which I can retrieve another JSON file with the same structure.
I'm able to retrieve the first set of "results" and then writing it into a Google Sheets; then I'm able to retrieve the "next_page_url" section but I'm having trouble appending the "next_page_url" data into the Google Sheets without headers.
Here is the code that I'm using in Google Apps Script:
Solved! Go to Solution.
Hi @miguel_krt ,
You’re close! The key issue is that results.concat(json["results"]) builds a combined array, but you never update the final sheet with all pages’ data — you only write once at the end and assume the array is shaped for setValues().
Here’s what to fix:
- Build a flat 2D array with only the data (no headers)
- Ensure you match the sheet’s expected format when writing
function updateJsonData() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
sheet.clearContents();
const baseUrl = '{baseurl}';
var url = '{url}';
var username = '{username}';
var password = '{password}';
const auth = "Basic " + Utilities.base64Encode(username + ":" + password);
let options = {
method: "get",
headers: { Authorization: auth },
muteHttpExceptions: true
};
let allResults = [];
let response = UrlFetchApp.fetch(url, options);
let json = JSON.parse(response.getContentText());
let results = json["results"];
allResults = allResults.concat(results);
while (json["next_page_url"]) {
let nextUrl = baseUrl + json["next_page_url"];
response = UrlFetchApp.fetch(nextUrl, options);
json = JSON.parse(response.getContentText());
allResults = allResults.concat(json["results"]);
}
// Convert objects to arrays (if needed)
let rows = allResults.map(obj => Object.values(obj));
// Write to sheet
if (rows.length > 0) {
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
}
}
User | Count |
---|---|
5 | |
1 | |
1 | |
1 | |
1 |