After spending a few hours on this, I've hit a roadblock. My app is returning a single JSON object with 4 datasets that I need to parse and use to create 3 charts and a table. However, I'm struggling to extract each part from the JSON structure which looks like:
{
"allele":{
"12426597":{
"??":4,
"CC":3,
"TT":4,
"CT":12
},
"878198":{
"??":4,
"AA":1,
"AC":15,
"CC":3
},
"6447271":{
"??":4,
"GG":14,
"AG":5
}
},
"haplo":{
"CT,AG,AC":3,
"TT,GG,AC":1,
"CC,GG,CC":1,
"TT,AG,CC":1,
"TT,GG,CC":1
},
"exercise":"p1"
}
My goal is to extract data for the three specific keys/IDs (12426597, 878198, 6447271) and create a bar chart for each of them using Highcharts. This requires some data transformation as well as formatting the keys and values into ordered arrays as per Highcharts API requirements.
I initially tried creating an array of the IDs like this:
var snpsObj = data.allele_frequency; // data returned from $.getJSON
var snpList = [];
for (prop in snpsObj) {
if (!snpsObj.hasOwnProperty(prop)) {
continue;
}
snpList.push(prop);
}
This gives me the desired array. Then I attempted to access the sub-keys using:
snpsObj.snpList[0];
Hoping it would return something similar to:
{
"CC" : 23,
"CT" : 36,
"TT" : 12,
}
Unfortunately, this approach did not work as expected. The closest I got was a return result of:
allele_frequency : [object Object ]
I know there must be a simple solution that I'm overlooking due to my current head cold-induced mental fog. Any suggestions on how to proceed?
Remember, Highcharts needs the keys and labels to be formatted in arrays such as:
categories: ['C', 'T'] data: [ 3, 9] // C=3, T=9