I love the JSON-as-a-parser hack.
In TSQL (MS SQL Server) it would run into the CTE recursion limit; I'm not sure if Postgres is any different in this regard. I suspect that this could be solved and optimized with a WHILE loop and, most likely, something similar to ORDPATH[1].
[1]: http://www.dbis.informatik.hu-berlin.de/fileadmin/lectures/W..., https://github.com/mejedi/libordpath