I have data in the following loose structure:

{
    "Bahrain":[
            {
                "date":"2010-10-10",
                "count":1000
            },
            {
                "date":"2010-10-11",
                "count":1202
            }

        ],
    "Morocco":[
            {
                "date":"2010-10-10",
                "count":1400
            },
            {
                "date":"2010-10-11",
                "count":1364
            }

        ]
}

The key to the data is that however I format the json, there will be some nesting. I am trying to create a multiline chart with a line for each country and the x-axis (date) and y-axis (count).

My questions is: How do I best structure the JSON (I create it in Python from a database) to allow for ease of access in D3?

Should I make it an array of objects like this:

[{"Country":"Bahrain","Values":[
        {
            "date":"2010-10-10",
            "count":1400
        },
        {
            "date":"2010-10-11",
            "count":1364
        }
    ]
},
{"Country":"Algeria","Values":[
        {
            "date":"2010-10-10",
            "count":1901
        },
        {
            "date":"2010-10-11",
            "count":890
        }
    ]
}]

So, object with nested arrays or array with nested objects, etc. etc.? The array with nested objects would make it easier to create shapes using .selectAll() Thanks

0 Answers