Dynamic Menu Bar using json data

I am using REST Adapter to create a Dynamic Menu Bar using external .json file. So far what I am able to achieve is that if the hierarchy of the json data is upto two levels I am able to display the correct menu, but what if the sub-menus have further sub-menus ? How to find the depth of the nested json objects to determine how many times should I run a loop to make the sub-menus