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?
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.
This means that deeply nested JSON objects can be used to attack implementations that use this approach (i.e. see this older
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.