I don't know if there's something like a JSON streaming parser (in Java there's a SAX streaming parser).
If it exists, a single loop plus a stack should be enough to get the desired output.
And the memory consumption would be O(1).
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.