Tania
Tania

Reputation: 1925

creating lists based on JSON data

I have this json array of dictionaries:

my_json == [
    { name: 'Allen', 'pass': 14, 'fail': 2 },
    { name: 'Meg', 'pass': 14, 'fail': 2 },
    { name: 'Catty', 'pass': 10, 'fail': 3 } 
]

I am using this data to create a highcharts bar chart with drilldown. I saw a fiddle demo and understand that my data series needs to be in the following format:

id:'pass'
data:[[Allen,14],[Meg,14],[Catty,10]
id:'fail'
data:[[Allen,2],[Meg,2],[Catty,3]

This looks like a list of lists and I'm absolutely clueless how to extract this sort of data from the above JSON. My need is just this. I will just have two series: Pass/Fail. On drilling down, I get the names of the children with their pass/fail ratio.

How to extract these lists from my JSON data?

Please let me know if you know any other easier way of drill charting this data with highcharts. Here's the fiddle: Chart fiddle

Upvotes: 1

Views: 52

Answers (1)

Khalid
Khalid

Reputation: 4808

This function might help you to get the data you need to that format

function formatData (json) {
    var pass = {id: 'pass', data: []};
    var fail = {id : 'fail', data: []};
    for(var index in json) {
        pass.data.push([json[index].name, json[index].pass]);
        fail.data.push([json[index].name, json[index].fail]);
    }
    retrun [pass, fail];
}

Upvotes: 2

Related Questions