Ask Your Question
4

What is the method for iterating through JSON URLs and retrieving data from them in Google Sheets using Apps Script?

asked 2021-06-11 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-02 02:00:00 +0000

woof gravatar image

Here is a sample code for iterating through JSON URLs and retrieving data from them in Google Sheets using Apps Script:

function getDataFromJSON() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var urls = ["https://jsonplaceholder.typicode.com/todos/1", "https://jsonplaceholder.typicode.com/todos/2"]; //array of JSON URLs
  for(var i = 0; i < urls.length; i++) { //iteration through URLs
    var response = UrlFetchApp.fetch(urls[i]); //fetch data from URL
    var data = JSON.parse(response.getContentText()); //parse the JSON data
    var row = [data.userId, data.id, data.title, data.completed]; //retrieve required data and store in an array
    sheet.appendRow(row); //append the data to the sheet
  }
}

In this code, we have created an array of JSON URLs (urls) that we want to retrieve data from. We are iterating through the URLs and fetching the data using the UrlFetchApp function. We are then parsing the JSON data using the JSON.parse() function and retrieving the required data. We are storing the data in an array (row) and appending it to the sheet using the appendRow() function.

You can modify this code as per your requirement to retrieve data from the JSON URLs and store them in the sheet.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-11 11:00:00 +0000

Seen: 14 times

Last updated: Aug 02 '21