I have some JSON data that I need to use in Tabulator table.
Simple example here from tabulator. http://tabulator.info/docs/4.2/quickstart I have to convert the JSON, but am struggling to find a dynamic solution where, say if I had an extra key with key values, it would still work with tabulator table. Here is my JSON data.
{
"Aspen_Premium": [
279245,
945317,
118388
],
"Brokerage_Upfront": [
825831,
141166,
507242
],
"Brokers": [
"JLT",
"Willis",
"BMS"
]
}
I need to have it in a JavaScript file like this.
var tabledata = [
{id: 1, Aspen_Premium: 279245, Brokerage_Upfront: 825831, Brokers: "JTL"},
{id: 2, Brokerage_Upfront: 945317, Brokerage_Upfront: 141166, Brokers: "Willis"},
{id: 3, Brokers: 118388, Brokerage_Upfront: 507242, Brokers: "BMS"},
]
var table = new Tabulator("#broker-table", {
height:205,
data:tabledata,
layout:"fitColumns",
columns:[
{title:"Aspen_Premium", field:"Aspen_Premium", width:150},
{title:"Brokerage_Upfront", field:"Brokerage_Upfront"},
{title:"Brokers", field:"Brokers"},
],
rowClick:function(e, row){ //trigger an alert message when the row is clicked
alert("Row " + row.getData().id + " Clicked!!!!");
},
});
My HTML is simply:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Check 1</title>
<link href="https://unpkg.com/[email protected]/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/js/tabulator.min.js"></script>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
</head>
<body>
<div id="broker-table"></div>
<script type="text/javascript" src="{{url_for('static',filename='js/checking1.js')}}"></script>
</body>
</html>
I'm using Python Flask. Hence the link for the javascript file, checking1.js has src="{{url_for('static', filename='js/checking1.js')}}">
Any help would be greatly appreciated as I've been struggling with this for several hours trying to do the conversion from JSON to the above hard coded version. I tried with a for loop, but kept getting issues.
Example of how I was grabbing my JSON data from http://127.0.0.1:5000/broker_data
$.get("/broker_data").then(function(data){
var columns = Object.keys(data);
var tabledata = [];
for(var i = 0; i < data[columns[0]].length; i++){
tabledata.push({"id": i+1, "Aspen": data[columns[0]][i]})
} console.log(tabledata);
});
The above is not ideal as I have to hardcode the "Aspen" key name.
To answer the part about converting the JSON data, this may help. If
var data = {
'Aspen_Premium':[279245,945317,118388],
'Brokerage_Upfront':[825831,141166,507242],
'Brokers':['JLT','Willis','BMS']
};
One way to get it into the desired format of:
[
{id: 1, Aspen_Premium: 279245, Brokerage_Upfront: 825831, Brokers: "JTL"},
{id: 2, Brokerage_Upfront: 945317, Brokerage_Upfront: 141166, Brokers: "Willis"},
{id: 3, Brokers: 118388, Brokerage_Upfront: 507242, Brokers: "BMS"},
]
Would be the following javascript:
//This gets the length of the array in the first element
var numItems = data[Object.keys(data)[0]].length;
var desiredJSON = [];
for( var itemIndex = 0; itemIndex < numItems; itemIndex++){
var doc = {};
doc.id = itemIndex;
for( dataElemIndex in data) {
doc[dataElemIndex] = data[dataElemIndex][itemIndex];
}
desiredJSON.push(doc);
}
After this loop, desiredJSON contains the array of documents you're trying to get. This code is assuming that all elements in data have the same length array.