I have an issue regarding the data from a JSON.
Link to example JSON: Example for Apple
I am using this code:
function yFinance(symbol) {
const url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + encodeURI(symbol)
+ '?modules=price,assetProfile,summaryDetail,incomeStatementHistory,'
+ 'balanceSheetHistory,defaultKeyStatistics,financialData,calendarEvents,'
+ 'recommendationTrend,upgradeDowngradeHistory,majorHoldersBreakdown'
;
const response = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const responseCode = response.getResponseCode();
if (responseCode === 200) {
const quote = JSON.parse(response.getContentText());
const paths = [
'summaryDetail/dividendRate/raw',
'summaryDetail/payoutRatio/raw'
];
return getMatchingValues(getPath(quote, 'quoteSummary/result/0'), paths);
}
else
{
return -1;
}
}
function getPath( obj, path ) {
if (!path || !obj) {
return null;
}
const parts = path.split('/');
const currentPath = parts[0];
const nextPath = parts.slice(1).join('/');
const currentObj = obj[currentPath];
// final path, exit here
if (nextPath.length === 0) {
return currentObj;
}
if (Array.isArray( currentObj )) {
// does an element exit at the next level
if ( currentObj[parts[1]] ) {
// will continue reading for 1 element
return getPath( currentObj, nextPath );
}
// return all the subpaths, skip the ones that are falsy
return currentObj.map( item => getPath( item, nextPath ) ).filter( v => v );
}
// get the next part of the object
return getPath( currentObj, nextPath );
}
function getMatchingValues( obj, paths ) {
return paths.flatMap( path => getPath( obj, path ));
}
The code is from this post
As I am not an expert with JSON, I have this issue:
Why can I get the data for ones but for others the results are null?
I do not grasp the diference.
I tried to change, in the code, "dividendRate/raw" by "enterpriseValue/raw" or "ebitda/raw" but they return null instead of a specific number.
Thank you
OK found the solution.
For "enterpriseValue":
For "ebitda/raw":