score:1

Accepted answer

This happens because requesting the JSON via AJAX is an asynchronous operation so the console.log is executed before what's inside the anonymous function.

You may want to look at promises in JavaScript or at least put your code inside that function so the result of the request is available to your code. At the simplest level this should work:

function draw(data) {
  var g = new dagreD3.graphlib.Graph().setGraph({});
  data.forEach(function (job) {
      console.log(job); // I do see job values correctly.
      g.setNode(job.child, { label: job.child });
      g.setEdge(job.parent,     job.child,     { label: job.path });
  });
  // carry on doing what you need in here...
}

d3.json("expectedOutput.json", draw);

Related Query