0

I'm trying to extract a specific value from this json file:

An example value I'm looking for is exDividendDate, fmt : 2020-09-24.

The code I've written to extract the value doesn't doesn't extract this or any other value and I'm not sure why. Any help would be greatly appreciated.

The error I get in the Google Apps Script is:

TypeError: Cannot read property 'earningsDate' of undefined (line 44, file "Stock Database"

function callAPI(symbol) { 
  
  // Call the API 
  var url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/'
  var modules = "?modules=calendarEvents"
  var response = UrlFetchApp.fetch(url + symbol + modules);
    
  // Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);
  console.log(data)
  return JSON.parse(json)
}


function displayFinancials() {
  
  // Load sheets
  var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Results"); 
  var modelSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Financial Ratios Model");  
  
  // Get model input data
  var company = "Apple"
  var symbol = "AAPL"
  
  // call the API
  var api = callAPI(symbol);
  var results = api[0]; 
  
  // Output the API result
  var output = [company, symbol, results.exDividendDate.fmt] 

  console.log(output);


  dataSheet.appendRow(output)
  
}
2
  • 1
    api according to your json file is not an array which might be why you get the undefined error Commented Nov 9, 2020 at 23:37
  • Thanks Steve, are you able to suggest a work around, I'm new to coding and am pretty much stuck on this one. Commented Nov 9, 2020 at 23:47

1 Answer 1

1

When I saw the JSON data, it seems that exDividendDate is callAPI(symbol).quoteSummary.result[0].calendarEvents. So how about the following modification?

From:

var results = api[0];

To:

var results = api.quoteSummary.result[0].calendarEvents;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.