Best practice to populate JSON page from sections

Hello;
I’m working on JSON API, that calls external JSON API and create a simple one using Hugo.
This API should be used by angularjs code to build category tree navigation.
my directory structure is

categories
    2
         _index.md
         3.md
         4.md

I want to have this structure when compiled:

categories
    index.json
    2
        index.json
        3
            index.json
        4
            index.json

So i created a sample code in single.json that get the current category and its sub categories info (id & name).
What’s the best way to federate this sub content in the main dir (for example for _index.md, that it have this format {id:“id”, name:“aname”, subitems [content of each sub item in json ]}
By the same, how to federate it in the main json dir ?
Hope it’s clear
Best regard