d3 - reading JSON data instead of CSV file d3 - reading JSON data instead of CSV file json json

d3 - reading JSON data instead of CSV file


You can use d3.entries() to turn an object literal into an array of key/value pairs:

var countsByDate = {'2000-01-01': 10, ...};var dateCounts = d3.entries(countsByDate);console.log(JSON.stringify(dateCounts[0])); // {"key": "2000-01-01", "value": 10}

One thing you'll notice, though, is that the resulting array isn't properly sorted. You can sort them by key ascending like so:

dateCounts = dateCounts.sort(function(a, b) {    return d3.ascending(a.key, b.key);});


Turn your .json file into a .js file that is included in your html file. Inside your .js file have:

var countsByDate = {'2000-01-01':10,...};

Then you can reference countsByDate....no need to read from a file per se.

And you can read it with:

var data = d3.nest().key(function(d) { return d.Key; })          .entries(json);       

As an aside....d3.js says it's better to set your json up as:

var countsByDate = [  {Date: '2000-01-01', Total: '10'},  {Date: '2000-01-02', Total: '11'},];