Jsondecode() -- why the 5 level nesting limit?

I am not in a position at the moment where I can test this empirically myself so I figured I ask and get some extra info at the same time…

According to the http.jsondecode() function’s documentation (http://electricimp.com/docs/api/http/jsondecode/)
“Tables and arrays can be nested up to five levels deep.”.

Why does this limit exist? It seems arbitrary but I’m sure there’s a good reason. The problem I’m going to run into inevitably is that the weather report data that I’ll eventually be using (when I’ve got that far in my project) will be nested 9 levels deep, even though the entire JSON string is less than 5kb long.

The documentation does not say what happens in the event that a JSON string with 6 or more nested layers is encountered. Presumably either the system throws an error, or the innermost layers are simply not decoded (which is ok because I can presumably just run the function again on the sub-set).

It occurred to me that this 5 layer limit might be an attempt by the developers to limit the amount of resources used on the server, which would be quite reasonable I think. However, how about having a second version of http.jsondecode() which has a limit on the total length of the JSON string, but no limit on the nesting depth? Might be a nice compromise.

Ah, looks like the documentation is a bit out-of-date. The current depth limit is 100, and is really only there to defuse potential denial-of-service problems from lunatically-deep JSON. So your weather service should be fine.


Ah, well that solves that then :slight_smile:

Thank you.