0

I'm trying to load .json into d3 but i'm not getting an array of objects.

My json structure is like this:

{
  "01": {
    "a": "7.9146197142424404",
    "b": "8", 
    "c": "64.52183289033583", 
    "d": "bar"
  }, 
  "02": {
    "a": "6.1448162722275468", 
    "b": "2", 
    "c": "82.657170917364141", 
    "d": "foo"
  }
}

Loading data:

        d3.json('mydata.json', function(data){

            console.log(data)


        });

When I console.log my data I get this:

https://www.dropbox.com/s/rgf38dkt6nrurei/Screen%20Shot%202014-07-05%20at%2019.26.41.png

Update:

So what I would want to do is add elements for each of the .json objects but it would be nice if I could make the first number the Key.

user2123511
  • 186
  • 1
  • 1
  • 7
  • possible duplicate of [Using an associative array as data for D3](http://stackoverflow.com/questions/9589768/using-an-associative-array-as-data-for-d3) –  Jul 05 '14 at 17:37
  • What do you want to happen? –  Jul 05 '14 at 23:44
  • So what I would want to do is add elements for each of the .json objects but it would be nice if I could make the first number the Key. – user2123511 Jul 06 '14 at 06:12

1 Answers1

0

You can use d3.entries() to turn your one giant object into an array containing the child objects.

When you do this, each of your array members will be an object with a .key property and a .value property. The key in your case will be '01' for the first member, '02' for the second, etc. The value will be the object with properties a, b, c, and d.

For example:

d3.json('mydata.json', function(data) {
  console.log(d3.entries(data));
});

will output:

[{
  key: "01",
  value: {
    a: "7.9146197142424404"
    b: "8"
    c: "64.52183289033583"
    d: "bar"
  }
},
{
  key: "02",
  value: {
    a: "6.1448162722275468"
    b: "2"
    c: "82.657170917364141"
    d: "foo"
  }
}]

Here's an example PLUNK

jshanley
  • 9,048
  • 2
  • 37
  • 44
  • Exactly what I needed. the data part in d3 can be a bit confusing for a beginner. I had worked with .csv files but not with .json. thanks a lot! – user2123511 Jul 06 '14 at 16:36