How to Flatten Deeply Nested JSON Objects in Non-Recursive Elegant Python
It is dangerous to flatten deeply nested JSON objects with a recursive python solution. Because the python interpreter limits the depth of stack to avoid infinite recursions which could result in stack overflows. And from performance standpoint, recursion is usually slower than an iterative solution.

The purpose of this article is to share an iterative approach for flattening deeply nested JSON objects with python source code and examples provided, which is similar to bring all nested matryoshka dolls outside for some fresh air iteratively.

Traditional recursive python solution for flattening JSON
The following function is an example of flattening JSON recursively. Code at line 16 and 20 calls function “flatten” to keep unpacking items in JSON object until all values are atomic elements (no dictionary or list).










