I have a CSV file containing hierarchical tree data as shown below:
industry,level1,level2,level3,name
Ecommerce,Web,,,Rakuten
Ecommerce,Crowdsourcing,,,Lancers
Social,Photo sharing,Deco apps,,Snapeee
Social,Photo sharing,Deco apps,Collage apps,DecoAlbum
Portals,,,,Yahoo Japan
The rows level1...level3
represent child nodes, and the row name
represents the bottom node. I am trying to use the d3.nest()
function to create a hierarchical JSON object. Specifically, I want to remove nodes where the level rows are empty. Here is the code I have so far:
d3.csv("data.csv", function(rows) {
sunburst_tree = d3.nest()
.key(function(d) { return d.industry; })
.key(function(d) { return d.level1; })
.key(function(d) { if (!(typeof d.level2 === 'undefined')) return d.level2; })
.entries(rows);
console.log(sunburst_tree);
});
This code generates a JSON object with empty keys, like this:
{"key":"Portals",
"values":[{"key":"",
"values":[{"key":"",
"values":[{"industry":"Portals","level1":"","level2":"","level3":"","name":"Yahoo Japan"}]
}]
}]
}
However, I would like to eliminate all empty sub-nodes, resulting in a JSON structure like this:
{"key":"Portals",
"values":[{"industry":"Portals",
"level1":"","level2":"","level3":"","name":"Yahoo Japan"}]}
}
How can I achieve this?