I would like to know the correct way to create a nested Json tree Structure object in javascript. data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or id)?
I have a very deep nested tree structure Json and I am given an object that can exist at any depth. I need to be able to iterate through all grandparent / parent / children nodes until I find the requested category, plus be able to capture its grandparent / parent / children categories all the way through.
//input data structure
JavaScript
x
62
62
1
[{
2
"Type": "grdparent1",
3
"name": "grdparent1",
4
"children": [{
5
"Type": "grdparent1",
6
"Id": 45,
7
"children": []
8
}, {
9
"Type": "grdparent1",
10
"Id": 46,
11
"children": [{
12
"Type": "parent1",
13
"Id": 54,
14
"children": [{
15
"Type": "child1",
16
"Id": 63,
17
"children": []
18
}, {
19
"Type": "child2",
20
"Id": 64,
21
"children": []
22
}]
23
}, {
24
"Type": "parent2",
25
"Id": 57,
26
"children": []
27
}]
28
}]
29
}, {
30
"Type": "grdparent2",
31
"name": "grdparent2",
32
"children": [{
33
"Type": "grdparent2",
34
"Id": 4,
35
"children": [{
36
"Type": "parent1",
37
"Id": 16,
38
"children": [{
39
"children": [],
40
"Type": "child1",
41
"Id": 28,
42
}]
43
}, {
44
"Type": "parent2",
45
"Id": 17,
46
"children": []
47
}]
48
}]
49
}, {
50
"Type": "grdparent3",
51
"name": "grdparent3",
52
"children": []
53
}, {
54
"Type": "grdparent4",
55
"name": "grdparent4",
56
"children": [{
57
"Type": "parent1",
58
"Id": 167,
59
"children": []
60
}]
61
}]
62
//output
JavaScript
1
42
42
1
[{
2
"grdparent1": [{
3
"Id": 45,
4
}, {
5
"Id": 46,
6
"parent1": [{
7
"Id": 54,
8
"child1": {
9
"Id": 63
10
}
11
}, {
12
"child2": {
13
"Id": 64
14
}
15
}]
16
}, {
17
"parent2": [{
18
"Id": 57
19
}]
20
}]
21
}, {
22
"grdparent2": [{
23
"Id": 4,
24
"parent1": [{
25
"Id": 16,
26
"child1": [{
27
"Id": 28
28
}]
29
}, {
30
"parent2": [{
31
"Id": 17
32
}]
33
}]
34
}, {
35
"grdparent4": [{
36
"parent1": [{
37
"Id": 167
38
}]
39
}]
40
}]
41
}]
42
Advertisement
Answer
Here is the code. You have the result in output variable:
JavaScript
1
113
113
1
var input = [{
2
"Type": "grdparent1",
3
"name": "grdparent1",
4
"children": [{
5
"Type": "grdparent1",
6
"Id": 45,
7
"children": []
8
}, {
9
"Type": "grdparent1",
10
"Id": 46,
11
"children": [{
12
"Type": "parent1",
13
"Id": 54,
14
"children": [{
15
"Type": "child1",
16
"Id": 63,
17
"children": []
18
}, {
19
"Type": "child2",
20
"Id": 64,
21
"children": []
22
}]
23
}, {
24
"Type": "parent2",
25
"Id": 57,
26
"children": []
27
}]
28
}]
29
}, {
30
"Type": "grdparent2",
31
"name": "grdparent2",
32
"children": [{
33
"Type": "grdparent2",
34
"Id": 4,
35
"children": [{
36
"Type": "parent1",
37
"Id": 16,
38
"children": [{
39
"children": [],
40
"Type": "child1",
41
"Id": 28,
42
}]
43
}, {
44
"Type": "parent2",
45
"Id": 17,
46
"children": []
47
}]
48
}]
49
}, {
50
"Type": "grdparent3",
51
"name": "grdparent3",
52
"children": []
53
}, {
54
"Type": "grdparent4",
55
"name": "grdparent4",
56
"children": [{
57
"Type": "parent1",
58
"Id": 167,
59
"children": []
60
}]
61
}];
62
63
var output = [];
64
65
for(var index = 0; index < input.length; index++) {
66
if(input[index].children.length > 0) {
67
var parsedObject = parseTopLevelItem(input[index]);
68
if(parsedObject) {
69
output[output.length] = parsedObject;
70
}
71
}
72
}
73
74
alert(JSON.stringify(output));
75
76
function parseTopLevelItem(item) {
77
var topLevelReturnObject;
78
if(item.children.length > 0) {
79
topLevelReturnObject = {};
80
for(var i = 0; i < item.children.length; i++) {
81
var parsedObject = parseChild(item.children[i]);
82
if(parsedObject) {
83
var key = parsedObject[0];
84
if(!topLevelReturnObject[key]) {
85
topLevelReturnObject[key] = [];
86
}
87
topLevelReturnObject[key][(topLevelReturnObject[key]).length] = parsedObject[1];
88
}
89
}
90
}
91
return topLevelReturnObject;
92
}
93
94
function parseChild(childElement){
95
var returnObject = [];
96
returnObject[0] = childElement.Type;
97
returnObject[1] = {};
98
returnObject[1].Id = childElement.Id;
99
100
for(var i = 0; i < childElement.children.length; i++) {
101
var parsedObject = parseChild(childElement.children[i]);
102
if(parsedObject) {
103
var key = parsedObject[0];
104
if(!returnObject[1][key]) {
105
returnObject[1][key] = [];
106
}
107
returnObject[1][key][(returnObject[1][key]).length] = parsedObject[1];
108
}
109
}
110
111
return returnObject;
112
}
113