txt = '{\n"nodes":[\n';
d3.selectAll("g.node").each(function(d) {
txt += '{"node":' + d.node + ', "name":"' + d.name + '"},\n'
})
txt = txt.substring(0, txt.length - 2); // strip last ,
txt += '\n],\n"links": [\n'
d3.selectAll(".link").each(function(d) {
txt += '{"source":' + d.source.node + ', "target":'
+ d.target.node + ',"value":' + d.value + '},\n'
})
txt = txt.substring(0, txt.length - 2); // strip last ,
txt += '\n]}'
console.log(txt);
};