I am having a node network in the neo4j database which includes multilevel nodes something like,
parent -> child -> sub child -> ...and so on
Now what I need is I need to write a query that will give me the response in the below format.
nodes = [
{
id: parent1,
children : [{
id: child1,
children: [
{
id: sub child1,
children: [... So on]
},
{
id: sub child2,
children: [... So on]
}
]
}]
},
{
id = parent2,
children : [...so on]
}
]
You can do something like:
Matching the top parents, which points no one, then using
apoc.path.expandConfig
to get their paths and thenapoc.convert.toTree
in order to get the tree-shape structure that you want.On a sample data:
It returns: