After a troublesome fight i almost figured how to convert a flat json file to a Hierarchical one. I didn\'t write the function by my own. I copied it from the below post.
var data = [
{ "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"A" },
{ "state": "UP", "district": "Agra", "block": "IradatNagar","school":"IPS","name":"B" },
{ "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"C" },
{ "state": "MP", "district": "Bhopal", "block": "chota_Bhopal","school":"DPS","name":"D" },
{ "state": "UP", "district": "Mathura", "block": "Farah","school":"HPS","name":"E" },
{ "state": "UP", "district": "Kanpur", "block": "Mania","school":"BPs","name":"F" },
{ "state": "UP", "district": "Agra", "block": "Arjun Nagar","school":"GPS","name":"G" },
{ "state": "MP", "district": "Gwalior", "block": "Surya Nagar","school":"DPS","name":"H" }
];
var newData = { name :"State", children : [] },
levels = ["state","district","block","school"];
data.forEach(function(d){
var depthCursor = newData.children;
levels.forEach(function( property, depth )
{
var index;
depthCursor.forEach(function(child,i)
{
if ( d[property] == child.name )
index = i;
});
if ( isNaN(index) )
{
depthCursor.push({name : d[property], children : []});
index = depthCursor.length - 1;
}
depthCursor = depthCursor[index].children;
if ( depth === levels.length - 1 )
{
depthCursor.push({ name : d.name});
}
});
});
console.log(newData);