tree2json: Correct and improve depth detection