Skip to content
Advertisement

Nested Tree structure object trying to extract and get information Json object

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

[{
    "Type": "grdparent1",
    "name": "grdparent1",
    "children": [{
        "Type": "grdparent1",
        "Id": 45,
        "children": []
    }, {
        "Type": "grdparent1",
        "Id": 46,
        "children": [{
            "Type": "parent1",
            "Id": 54,
            "children": [{
                "Type": "child1",
                "Id": 63,
                "children": []
            }, {
                "Type": "child2",
                "Id": 64,
                "children": []
            }]
        }, {
            "Type": "parent2",
            "Id": 57,
            "children": []
        }]
    }]
}, {
    "Type": "grdparent2",
    "name": "grdparent2",
    "children": [{
        "Type": "grdparent2",
        "Id": 4,
        "children": [{
            "Type": "parent1",
            "Id": 16,
            "children": [{
                "children": [],
                "Type": "child1",
                "Id": 28,
            }]
        }, {
            "Type": "parent2",
            "Id": 17,
            "children": []
        }]
    }]
}, {
    "Type": "grdparent3",
    "name": "grdparent3",
    "children": []
}, {
    "Type": "grdparent4",
    "name": "grdparent4",
    "children": [{
        "Type": "parent1",
        "Id": 167,
        "children": []
    }]
}]

//output

[{
    "grdparent1": [{
        "Id": 45,
    }, {
        "Id": 46,
        "parent1": [{
            "Id": 54,
            "child1": {
                "Id": 63
            }
        }, {
            "child2": {
                "Id": 64
            }
        }]
    }, {
        "parent2": [{
            "Id": 57
        }]
    }]
}, {
    "grdparent2": [{
        "Id": 4,
        "parent1": [{
            "Id": 16,
            "child1": [{
                "Id": 28
            }]
        }, {
            "parent2": [{
                "Id": 17
            }]
        }]
    }, {
        "grdparent4": [{
            "parent1": [{
                "Id": 167
            }]
        }]
    }]
}]

Advertisement

Answer

Here is the code. You have the result in output variable:

    var input = [{
        "Type": "grdparent1",
        "name": "grdparent1",
        "children": [{
            "Type": "grdparent1",
            "Id": 45,
            "children": []
        }, {
            "Type": "grdparent1",   
            "Id": 46,
            "children": [{
                "Type": "parent1",
                "Id": 54,
                "children": [{
                    "Type": "child1",
                    "Id": 63,
                    "children": []
                }, {
                    "Type": "child2",
                    "Id": 64,
                    "children": []
                }]
            }, {
                "Type": "parent2",
                "Id": 57,
                "children": []
            }]
        }]
    }, {
        "Type": "grdparent2",
        "name": "grdparent2",
        "children": [{
            "Type": "grdparent2",
            "Id": 4,
            "children": [{
                "Type": "parent1",
                "Id": 16,
                "children": [{
                    "children": [],
                    "Type": "child1",
                    "Id": 28,
                }]
            }, {
                "Type": "parent2",
                "Id": 17,
                "children": []
            }]
        }]
    }, {
        "Type": "grdparent3",
        "name": "grdparent3",
        "children": []
    }, {
        "Type": "grdparent4",
        "name": "grdparent4",
        "children": [{
            "Type": "parent1",
            "Id": 167,
            "children": []
        }]
    }];

    var output = [];

    for(var index = 0; index < input.length; index++) {
        if(input[index].children.length > 0) {
            var parsedObject = parseTopLevelItem(input[index]);
            if(parsedObject) {
                output[output.length] = parsedObject;
            }
        }
    }

alert(JSON.stringify(output));

    function parseTopLevelItem(item) {
        var topLevelReturnObject;
        if(item.children.length > 0) {
            topLevelReturnObject = {};
            for(var i = 0; i < item.children.length; i++) {
                var parsedObject = parseChild(item.children[i]);
                if(parsedObject) {
                    var key = parsedObject[0];
                    if(!topLevelReturnObject[key]) {
                        topLevelReturnObject[key] = [];
                    }
                    topLevelReturnObject[key][(topLevelReturnObject[key]).length] = parsedObject[1];
                }
            }
        }
        return topLevelReturnObject;
    }

    function parseChild(childElement){
        var returnObject = [];
        returnObject[0] = childElement.Type;
        returnObject[1] = {};
        returnObject[1].Id = childElement.Id;

        for(var i = 0; i < childElement.children.length; i++) {
                var parsedObject = parseChild(childElement.children[i]);
                if(parsedObject) {
                    var key = parsedObject[0];
                    if(!returnObject[1][key]) {
                        returnObject[1][key] = [];
                    }
                    returnObject[1][key][(returnObject[1][key]).length] = parsedObject[1];
                }
        }

        return returnObject;
    }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement