'Need to merge jsons representing parent child relationship
I have json values to be depicted in jstree format
I want a union of json1 and json2 like the following merged json

The data for this jstree is in the following format json1:
[
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "xxxxx" },
]
json2:
[
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "yyyyy" },
]
Desired output:
[
{ "id" : "tree1ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "tree1ajson3", "parent" : "tree1ajson2", "text" : "Child 1" },
{ "id" : "tree1ajson4", "parent" :"tree1ajson2", "text" : "xxxxx", "supportedTree":"tree1"},
{ "id" : "tree2ajson4", "parent" : "tree1ajson2", "text" : "yyyyy", "supportedTree":"tree2" },
]
Conditions:
- A parent can have any depth of descendants
- In case, a folder is present only on one tree, itself and all its descendants should be added to mergedtree with the "supportedTree" key and value.
- ids can overlap. Eg: xxxxx and yyyyy have the id ajson4 in our example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
