I am receiving the following JSON from back end
Right now there are two strike prices 6000 and 7000 and CE and PE open interest values for each strike price
[{
"CE": "601650",
"strike_pr": "6000",
"PE": "1196350"
}, {
"CE": "2553975",
"strike_pr": "7000",
"PE": "6069025"
}]
I am trying to generate a google visulization bar chart with this values as shown in this fiddle
http://jsfiddle.net/7wYP2/453/
Could you please how can i build below format JSON Array form the above JSON
var data = google.visualization.arrayToDataTable([
['STRIKE_PR', 'CE', 'PE'],
['6000 ', 601650, 1196350],
['7000', 2553975, 6069025]
]);
You can try something like this:
Note: This answer assumes all objects will have same properties.
var data = [{
"CE": "601650",
"strike_pr": "6000",
"PE": "1196350"
}, {
"PE": "6069025",
"CE": "2553975",
"strike_pr": "7000",
}];
var keys = Object.keys(data[0]);
var result = data.map(function(o){
return keys.map(function(k){
return o[k];
});
});
result.unshift(keys);
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
var data = [{
"CE": "601650",
"strike_pr": "6000",
"PE": "1196350"
}, {
"CE": "2553975",
"strike_pr": "7000",
"PE": "6069025"
}];
var keys = Object.keys(data[0]);
var result = [];
result.push(keys);
data.forEach(function(o){
var _tmp = [];
keys.forEach(function(k){
_tmp.push(o[k])
});
result.push(_tmp);
});
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
var data = [{
"CE": "601650",
"strike_pr": "6000",
"PE": "1196350"
}, {
"CE": "2553975",
"strike_pr": "7000",
"PE": "6069025"
}];
var keys = Object.keys(data[0]);
var result = data.map(function(o){
var _tmp = [];
keys.forEach(function(k){
_tmp.push(o[k])
});
return _tmp;
});
result.unshift(keys);
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments