Json Data to Nested Json Data

can anyone help me out,
i tried with some other examples but i didn’t get them.
I’m Getting operation name inside the childnode items But i’m not getting the child nodes inside the parent Node Items.
This is my input

var arr = [
                { PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Update" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "View" },
                { PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Delete" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "Insert" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Update" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "View" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Insert" },
                { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Delete" },
                { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Update" },
                { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "View" },
                { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Insert" },
                { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Delete" }
                 ],

I need to get output as Nested Json,

[{
        text: "Resource Management",
        items: [{
            text: "Manpower",           
            items: [{
                text: "Update",
            }, {
                text: "Delete",
            }, {
                text: "Insert",
            }, {
                text: "View",
            }]
        }, {
            text: "Vehicles",
            items: [{
                text: "Update",
            }, {
                text: "Delete",
            }, {
                text: "Insert",
            }, {
                text: "View",
            }]
        }]        
    }, {
        text: "Contacts",
        items: [{
            text: "Manage Customers",
            items: [{
                text: "Update",
            }, {
                text: "Delete",
            }, {
                text: "Insert",
            }, {
                text: "View",
            }]
        }]
    }]

I have tried like this,


<script type="text/javascript">
    'use strict';

    var arr = [
            { PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Update" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "View" },
            { PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Delete" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "Insert" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Update" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "View" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Insert" },
            { PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Delete" },
            { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Update" },
            { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "View" },
            { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Insert" },
            { PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Delete" }
             ];

    function getOrCreateProperty(parentObject, propertyName)
    {
        var propertyNameSlug = propertyName.toLowerCase().replace(' ', '-');
        //Check if item doesn't exist yet
        if (!parentObject[propertyNameSlug]) {
            //Create new item
            parentObject[propertyNameSlug] = {
                text: propertyName,
                items: []
            };
        }
        return parentObject[propertyNameSlug];
    }

    var newData = {};

    for (var item of arr) {

        //Get parent item
        var parent = getOrCreateProperty(newData, item.PARENTNODE);

        //Get child item
        var child = getOrCreateProperty(parent, item.CHILDNODE);

        //Add child item data
        child.items.push({text: item.OPERATIONNAME});
    }

    console.log(newData);

</script>

This is my fiddle [ http://jsfiddle.net/m9c771LL/69/]

Thanks in Advance!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.