I have a script that looks through a JSON structure. Every now and again, a particular key is missing. When these instances occur, I need the script to continue rather than stop. Here is my code:
for i = 1:length(marketCatalogueResult)
marketResults = getMarketOffers(currentCatalogue[i,1])
push!(lastTradeP1, marketResults[1]["runners"][1]["lastPriceTraded"])
push!(lastTradeP2, marketResults[1]["runners"][2]["lastPriceTraded"])
end
end
The error I sometimes get is:
KeyError: key "lastPriceTraded" not found
How can I amend my code to handle the stated error?
You may want to consider using missing instead, which is the Julian way to indicate missing data. NaN has a very specific meaning, which is that it is the result of a computation where zero was divided by zero or infinity was subtracted from itself.
While I agree with the semantics, the temptation of just keeping things simple with a single primitive type, with no unions or risk of type instability should not be underestimated. I have some data wrangling code that deal with Float64 columns that will never have a “true” NaN (things like X_time where X is a procedure that is not always called) so it is very useful to just use NaNs as a sentinel value.