you can eventually test:
if (t.pending_payments[i] == nil) then break end
inside your loop (if you assume that the list of months has no "hole", but you don't want to set a limit for your for loop.
Anyway the for loop should better be using ipairs() so that you can still accept "holes" in months:
for i,payment in in ipairs(t) do
if limit and i > limit then break end
total = total + payment.amount;
end
(your sample only shows named months probably of the same year, but the actual data may be an arbitrary month in ISO format "yyyy-mm" or using a second property for the year. In which case the "limit" should be able to compute if the date match the limit which could accept either a maximum number of months over possibly several years, or a date limit also in "yyyy-mm" format).
Also nothing indicates that the JSON data is necessary ordered: eahc payment has its own month, they may occur in arbitrary order, or there would be several payments for the same month in different rows...
So check your data model for your JSON source!