I have a single level, adjacency list that I build from a relational database into a JSON object in a Rails 4 project. It looks like
{
"6": {
"children": [
8,
10,
11
]
},
"8": {
"children": [
9,
23,
24
]
},
"9": {
"children": [
7
]
},
"10": {
"children": [
12,
14
]
...
}
Now I want to get this into a JSON structure to be consumed by jsTree, that looks like
{
id: "6",
children: [
{ id: "8", children: [ { id: "9", children: [{id: "7"}] }]
{ id: "10", children: [ { id: "12",...} {id: "14",...} ] }
...and so on
}
The problem I am facing with building this kind of a tree is with the issue of backtracking over the nested levels of JSON. The examples in algorithms text books are not enough to match up with my experience where the backtracking issue is simply handled by pushing some elemental data like a number or string to a stack.
Any help with a practical approach to build such a tree is appreciated.