featherz
featherz

Reputation: 169

reformatted data into JSON from CSV, can't get parsing right in D3

I had a graph working with CSV but we've decided to get it up and running with JSON instead. I'm having a difficult time parsing the nested pieces so that I can use them.

Here is the fiddle link and the code block (fiddle is not working) - I'm not looking for someone to get the whole thing working... I just need help with the parsing and example of using the data.

http://jsfiddle.net/Mmdc8/

d3.json("https://dl.dropboxusercontent.com/u/23726217/progtime.json", function (error, jsondata) {            
        var studyData = d3.entries(jsondata);
        console.log(JSON.stringify(studyData[0]));

        studyData.forEach(function (d) {
            d.value.name = d.value.name
        });
        console.log(studyData.value.name);

Upvotes: 2

Views: 248

Answers (1)

Pablo Navarro
Pablo Navarro

Reputation: 8264

Your JSON file has the following structure:

- details: [...]
- annotations: [...]
- dates: [...]

Each one of this fields, contains an array with objects. To access the data, you don't need to use entries:

d3.json(jsonUrl, function(error, jsondata) {

   // Handle errors getting or parsing the data
   if (error) { return error; }

   // Parse the dates
   var startDate = new Date(jsondata.dates.StartDate),
       endDate = new Date(jsondata.dates.EndDate);

   // It will print the array of objects
   console.log(jsondata.details); 

   // Print the start and end dates
   console.log([startDate, endDate]);
});

EDIT: Added access to the dates.

Upvotes: 1

Related Questions