Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing local json file using d3.json does not work

Tags:

json

d3.js

I try to import a local .json-file using d3.json().

The file filename.json is stored in the same folder as my html file.

Yet the (json)-parameter is null.

d3.json("filename.json", function(json) {     root = json;     root.x0 = h / 2;     root.y0 = 0;});     . . .  } 

My code is basically the same as in this d3.js example

like image 734
Aleks G Avatar asked Jun 20 '13 12:06

Aleks G


People also ask

What is d3 JSON?

d3.json() Sends http request to the specified url to load . json file or data and executes callback function with parsed json data objects. d3.tsv() Sends http request to the specified url to load a .

Is the syntax to read JSON data in d3?

json() function is used to fetch the JSON file. If this function got an init parameter, then this is called along with the fetch operation. Syntax: d3.


1 Answers

If you're running in a browser, you cannot load local files.

But it's fairly easy to run a dev server, on the commandline, simply cd into the directory with your files, then:

python -m SimpleHTTPServer 

(or python -m http.server using python 3)

Now in your browser, go to localhost:3000 (or :8000 or whatever is shown on the commandline).


The following used to work in older versions of d3:

var json = {"my": "json"}; d3.json(json, function(json) {     root = json;     root.x0 = h / 2;     root.y0 = 0; }); 
like image 186
mb21 Avatar answered Sep 19 '22 15:09

mb21