Not able to parse "price_change_pct" using google app script

Hi !

I hate ImportJSON (it automatically pulls data as soon as sheet is opened)… I prefer javascript as it allows more control on data.

I wish to compare many new coins…hence trying to pull max data manually once using UI button into a single sheet.

My script code works perfectly well to grab 10 columns and push the values to the sheet…but it throws error “TypeError: Cannot read property ‘price_change_pct’ of undefined”

Code is:

var url2 = “https://api.nomics.com/v1/currencies/ticker?key=my-secret-key&interval=ytd&status=active&sort=rank”;
var result2 = UrlFetchApp.fetch(url2);
var dataAll = JSON.parse(result2.getContentText());
var rows = ,data2;
var sh2 = SpreadsheetApp.getActive().getSheetByName(‘CData’)
for (var i in dataAll) {
data2 = dataAll[i];
rows.push([data2.id, data2.symbol, data2.name, data2.max_supply, data2.market_cap_dominance, data2.num_exchanges, data2.rank, data2.rank_delta, data2.high, data2.high_timestamp, data2.ytd.price_change_pct, data2.ytd.market_cap_change_pct]);
}
dataRange2 = sh2.getRange(2, 1, rows.length, 12);
dataRange2.setValues(rows);

although console.log shows many correct entries for data2.ytd.price_change_pct …before the error pops?

I tried various solutions. even used interval 1d and code: dataAll[i][“1d”].price_change_pct but nothing is working even if else statement for checking undefined…I guess some Coins have missing ytd in JSON…causing script to throw error.

Could you suggest what I am missing here? plus I cannot find my API usage stats anywhere on the website? I understand & wish to honor 1 call/sec…but Is there limitation to no. of rows or columns that one can request in a single call?

Thanks & Regards

Not all assets will have a ytd price change. The change is based on the period from the beginning of the year until now, compared to the period before that of equal length.

So, today is 9 months into the year so it would compare the past 9 months against the previous 9 to 18 month period. If the coin was not active in that period it can’t provide a price or volume change.

You should put guards in your code so that if the field is not present the column is blank.

You’ll get HTTP 429 rate limit errors if you exceed the rate limit, but it is reset every second, so there’s no punishment. If you start seeing those errors then you’ll know you need to work on your request timings.

Thanks for clarifying YTD…but don’t you think that generally JSON structure should be same throughout on any endpoint …instead of missing field YTD… it should be present with a blank or string “NA” (not applicable).

Nomics has one of the best reliable crypto data …but strangely I feel it’s JSON fields & structure are not up to the mark…I had given feedback to Sir Clay Collins regarding ID field which instead of being unique (eg, unique numbers in coinmarketcap …even I have seen some API using MD5 hash …overkill I guess) they are almost as same as coin symbols which makes it more prone to user error & hard to find the exact coins if they have same symbol…thereby we have to pull more data like name, description from the API to pinpoint to the exact project.

I agree on all points. This API is as old as Nomics itself and we have strongly opposed any breaking changes over the years in order to keep compatibility for all users. We have quite a list of thing’s we’d like to improve, and how we do intervals and IDs is on it! Thanks for the feedback.