I'd like to convert this object that I receive from server to a JSON file so that I can use it in d3.js chart:
data = {
"dog ":"5",
"cat ":"4",
"fish ":"12",
}
The output should be:
{
"name" : "animal",
"children" : [
{"name":"dog", "value": 5},
{"name":"cat", "value": 4},
{"name":"fish", "value": 10}
]
}
What I came up with is:
var jsonText = [] ;
for ( key in data) {
jsonText.push({name : key.trim(), value : parseInt(data[key])});
}
But when I try to print out the object, I receive:
[object Object],[object Object],[object Object]
In addition to that I have no clue how to add other attributes to the JSON file. So appreciate your hints.
console.log(JSON.stringify(object));name:animaldata from? The data I'm seeing from your snippet never tells you that.