Problems with parsing JSON for D3
<p>I am building an app with Flask and Python, and I want to pass some of my Python-generated results into JSON, so that they can be visualized with D3 under a container fluid. To do this, I am trying to use the Jinja method <code>var myjson = {{jsonDict|tojson }};</code>. Here <code>jsonDict</code> is a variable in my Python code that is a string of a dict, where single quotes have been replaced with double quotes with a regular expression, so that it looks like proper JSON. I am also using the JS method <code>root = JSON.parse( myjson );</code>. I believe that the combination of these two should solve my problem, but when I run the code, however I am getting the error:</p>

<pre><code>(index):2222    
Uncaught SyntaxError: Unexpected token y in JSON at position 0 
uo  @   d3.v3.min.js:3
i   @   d3.v3.min.js:1
</code></pre>

<p>Here is the D3 template that I am trying to use:
<a href="http://bl.ocks.org/mbostock/7607535" rel="nofollow">http://bl.ocks.org/mbostock/7607535</a></p>

<p>Here is my implementation of this D3 (just the relevant script):</p>

<pre><code>&lt;script&gt;

var margin = 20,
    diameter = 960;

var color = d3.scale.linear()
    .domain([-1, 5])
    .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
    .interpolate(d3.interpolateHcl);

var pack = d3.layout.pack()
    .padding(2)
    .size([diameter - margin, diameter - margin])
    .value(function(d) { return d.size; })

var svg = d3.select("#dan").append("svg") //#dan is name of my container fluid
    .attr("width", diameter)
    .attr("height", diameter)
  .append("g")
    .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");

//this is the part of the code I have added //
var myjson = {{ jsonDict|tojson }};
root = JSON.parse( myjson );
//this is the part of the code I have added //    

d3.json("root", function(error, root) {
  if (error) throw error; //this is index 2222

  var focus = root,
      nodes = pack.nodes(root),
      view;

  var circle = svg.selectAll("circle")
      .data(nodes)
    .enter().append("circle")
      .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
      .style("fill", function(d) { return d.children ? color(d.depth) : null; })
      .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });

  var text = svg.selectAll("text")
      .data(nodes)
    .enter().append("text")
      .attr("class", "label")
      .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
      .style("display", function(d) { return d.parent === root ? "inline" : "none"; })
      .text(function(d) { return d.name; });

  var node = svg.selectAll("circle,text");

  d3.select("#dan")
      .style("background", color(-1))
      .on("click", function() { zoom(root); });

  zoomTo([root.x, root.y, root.r * 2 + margin]);

  function zoom(d) {
    var focus0 = focus; focus = d;

    var transition = d3.transition()
        .duration(d3.event.altKey ? 7500 : 750)
        .tween("zoom", function(d) {
          var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
          return function(t) { zoomTo(i(t)); };
        });

    transition.selectAll("text")
      .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
        .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
        .each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
        .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
  }

  function zoomTo(v) {
    var k = diameter / v[2]; view = v;
    node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
    circle.attr("r", function(d) { return d.r * k; });
  }
});

d3.select(self.frameElement).style("height", diameter + "px");

&lt;/script&gt;
</code></pre>

<p>As you can see, I replaced the original JS lines from the D3 code:</p>

<pre><code>d3.json("flare.json", function(error, root) {
  if (error) throw error;
</code></pre>

<p>with:</p>

<pre><code>var myjson = {{ jsonDict|tojson }};
    root = JSON.parse( myjson );   

    d3.json("root", function(error, root) {
</code></pre>

<p>As I have my code now, if I inspect the page, the <code>svg</code> shows up on the webpage in the correct place, but it is blank.</p>

<p>I am new to D3 and Javascript. Any help would be much appreciated! Thank you! </p>

<p>EDIT - console logs</p>

<p>if I do console.log(myjson), the console prints the string of the JSON properly (see below)</p>

<p>if I do console.log(root), the console prints</p>

<pre><code>Object {children: Array[2], name: "flare"}
children:Array[2]
name:"flare"
__proto__:Object
__defineSetter__: __defineSetter__()
__lookupGetter__:__lookupGetter__()
__lookupSetter__:__lookupSetter__()
constructor:Object()
hasOwnProperty:hasOwnProperty()
isPrototypeOf:isPrototypeOf()
propertyIsEnumerable:propertyIsEnumerable()
toLocaleString:toLocaleString()
toString:toString()
valueOf:valueOf()
get __proto__:__proto__()
set __proto__:__proto__()
</code></pre>

<p>So it seems that the <code>JSON.parse</code> method is failing me somehow.</p>

<p>EDIT -- my JSON string that is being passed from Python into <code>var myjson</code></p>

<pre><code>{"name": "flare", "children": [{"name": "concept0", "children": [{"name": "intermediate host", "size": 700}, {"name": "abstrusus brevior", "size": 700}, {"name": "stage larva", "size": 700}, {"name": "anterior extremity", "size": 700}, {"name": "crenosoma vulpi", "size": 700}]}, {"name": "concept1", "children": [{"name": "infected cat", "size": 700}, {"name": "abstrusus infection", "size": 700}, {"name": "domestic cat", "size": 700}, {"name": "feline aelurostrongylosis", "size": 700}, {"name": "cat infect", "size": 700}]}]}
</code></pre>

<p>pagesource for <code>console.log(myjson)</code></p>

<pre><code>{"name": "flare", "children": [{"name": "concept0", "children": [{"size": 700, "name": "intermediate host"}, {"size": 700, "name": "abstrusus brevior"}, {"size": 700, "name": "stage larva"}, {"size": 700, "name": "anterior extremity"}, {"size": 700, "name": "crenosoma vulpi"}]}, {"name": "concept1", "children": [{"size": 700, "name": "infected cat"}, {"size": 700, "name": "abstrusus infection"}, {"size": 700, "name": "domestic cat"}, {"size": 700, "name": "feline aelurostrongylosis"}, {"size": 700, "name": "cat infect"}]}]}
</code></pre>

<p>EDIT - I think the problem can be seen in <code>console.log(root)</code>. I've checked other D3 visualizations and the log should typically look like this:</p>

<pre><code>Object {name: "flare", children: Array[5]}
children: Array[5]
depth:0
name:"flare"
r:470
value:21000
x:470
y:470
__proto__:Object
</code></pre>