0

I was watching a video on a website that said json objects can be nested deeply. How far can a JSON object be nested before it will create an error?

  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Apr 15 '15 at 22:33
  • 1
    I tested creating an object 10000000 (ten million) levels deep and it worked (at least in Chrome) but trying the same code on an object 100000000000 (hundred billion) levels deep crashed on console.log() (on Chrome). I also lost my jsfiddle due to the crash so we know that Google Chrome would start crashing when you try to console.log an object of between ten million and a hundred billion levels deep. – slebetman Apr 16 '15 at 4:10
  • I should note that in my test above, creating an object a hundred billion levels deep worked. It only crashed when I try to console.log it. – slebetman Apr 16 '15 at 4:11
  • 1
    If you get so deep many times you are actually dealing with different data. For example: Graph data or time series data. There are better formats to store that and that's about the only reason why you might run into this issue. – Luc Franken Apr 16 '15 at 8:16
8

There is no theoretical limit to how deep JSON objects can be nested, but there usually is a practical limit based on the decoder being used. For example, PHP's json_decode() has a default limit of 512 levels, though it can be adjusted. Read the documentation for the code using the JSON to determine the max depth.

If your JSON is actually hitting depth limits, you probably need to reconsider how your JSON is structured.

3

Many JSON parsers and formatters use recursion (i.e., ArduinoJSON and the IBM DastaPower Gateway).

This means that deeply nested JSON objects can be used to attack implementations that use this approach (i.e. see this older jansson issue)).

Originally, the JSON testing suite offered by json.org was testing to make sure parsers would fail at 22 levels of nesting.

This also makes sense where data accessing is concerned. Accessing nested data is less readable (and less maintainable) than multiple storage objects.

So, although there's no official limits to JSON nesting levels, in order to allow your data to be portable and safe, I would not recommend any nesting level above 16.

IMHO, the 16-31 nesting levels should be considered a hazard zone and anything above 32 nesting levels should be considered as a design error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.