I've stored a tree that contains multiple nodes. Every record in that table represents a node and its parent-node, as follows:
node_id | parent_id |
---|---|
A | null |
B | A |
C | A |
D | B |
E | B |
As a result, the visual tree would look like this: tree-nodes
My goal is to create a function that'll hold the JSON path for every leaf in the tree. So for my current table, the result should behave as shown below:
leaf_id | json_path |
---|---|
C | {"name": "A", "children": [{ "name": "C", "children": [] }] } |
D | {"name": "A", "children": [{ "name": "B", "children": [{ "name": "D", "children": [] }] }] } |
E | {"name": "A", "children": [{ "name": "B", "children": [{ "name": "E", "children": [] }] }] } |
There's already a question with a function that does the format I'm trying to achieve (link below): nested-json-object. However, the written function selects the entire tree. Therefore, as I mentioned above, I need the path of every leaf node.