Search notes:

D3.js - Reading data

JSON

A simple example that tries to demonstrate how a JSON document can be read with D3.js.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Reading JSON with D3.js</title>

</head>
<body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>

<script>

d3.json('data.json').
   then(function(jsn){
      alert("txt = " + jsn.txt + "\n" + "num = " + jsn.num);
   }).
   catch(function(err) {
      alert("Error: " + err);
   });

</script>

</body>
</html>
Github repository about-d3.js, path: /data/json.html
This is the document:
{
  "num":  42,
  "txt": "Hello world"
}
Github repository about-d3.js, path: /data/data.json
In order to test this example on a local computer, a CORS capable webserver must be started (for example with Python).

See also

D3.js

Index