1

I am using nested JSON file in my data visualization using d3.js. I was referring to a previous Smilar Queastion. I created a jsfiddle of the answer of the question to see whether things are working. But i am confused why the code is not working. I have similar type of problem in my project. How can i solve that. Here is sample code I am using for printing the data in the form of a list

d3.select("body").append("ul").selectAll("li")
.data(data).enter().append("li").each(function() {
  var li = d3.select(this);
  li.append("p")
      .text(function(d) { return d.date; });
  li.append("ul").selectAll("li")
      .data(function(d) { return d.hours; }) // second level data-join
    .enter().append("li")
      .text(function(d) { return d.hour + ": " + d.hits; });
});
1
  • Solved. Replace outside second brackets with third brackets. Then it works fine Commented Jun 10, 2015 at 14:32

1 Answer 1

0

It looks like the JSON wasn't formed correctly. I fixed the JSON like this -

    var data=[{ "date": "20120927", 
  "hours": [
           { "hour": 0, "hits": 823896 }, 
           { "hour": 1, "hits": 654335 }, 
           { "hour": 2, "hits": 548812 }, 
           { "hour": 3, "hits": 512863 }, 
           { "hour": 4, "hits": 500639 }
           ],
  "totalHits": "32,870,234", 
  "maxHits": "2,119,767", 
  "maxHour": 12, 
  "minHits": "553,821", 
  "minHour": 3 },
{ "date": "20120928", 
  "hours": [ 
           { "hour": 0, "hits": 1235923 }, 
           { "hour": 1, "hits": 654335 }, 
           { "hour": 2, "hits": 1103849 }, 
           { "hour": 3, "hits": 512863 }, 
           { "hour": 4, "hits": 488506 }
           ],
  "totalHits": "32,870,234", 
  "maxHits": "2,119,767", 
  "maxHour": 12, 
  "minHits": "553,821", 
      "minHour": 3 }];

Also, I think there was a problem with the HTTP d3.js library not being loaded. I added an external HTTPS reference and it is working.

Here is a working version of your fiddle - https://jsfiddle.net/ferlin_husky/wzuvob6m/

Sign up to request clarification or add additional context in comments.

4 Comments

What is the error in my json. Doesn't it take double quotations?
Yap got it. I need to add external resource instead thanks. My code is working with double quotations also. Here is the updated fiddle. jsfiddle.net/pbf1u640/6
Double quotes are ok. I updated my answer so it was more clear.
The problem was just with the second brackets outside. Those need to be replaced with third brackets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.