1

I am generating an zoomable chart by using d3.js.

chart data is being received as json object. how to change the following code to work with JSON object

d3.json("mydata.json", function(json) {
//TODO: 
});

instead of file name "msdata.json" I want to use Json object. please help me how to do this.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
Rajesh Kumar
  • 349
  • 3
  • 18
  • You have to explain what you mean by "JSON object", because [there is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). Maybe you mean an object (`var obj = {foo: 'bar'};`) or JSON as a string (`var json = '{"foo": "bar"}';`) ? – Felix Kling Jul 15 '14 at 06:28
  • Maybe this helps you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Felix Kling Jul 15 '14 at 06:33
  • refer http://stackoverflow.com/questions/20834760/how-to-read-json-data-from-same-json-for-tree-layout-insteadof-reading-from-othe/20835211#20835211 – Manoj Jul 15 '14 at 07:33

1 Answers1

2

If you already have a JSON string (there's no such thing as JSON object) in the script, you can remove your d3.json request and use JSON.parse to convert the JSON string to an object (usually it's an array) like so:

var data = JSON.parse(yourJSONString);

Then you can use it to compute data joins or whatever you want to do with it in d3:

someSelection.data(data).enter()... // etc.
Oleg
  • 9,341
  • 2
  • 43
  • 58