I am trying to convert output from SQL Query which comes as a table. I have converted the table as JSON. Now I am in a process of converting the JSON to HTML Table so that I can use it for reporting.
Script is given below,
var value = '{"root":{"row":[{"DatabaseID":"21","fileid":"1","databaseName":"AutomationPortal","FileLogicalName":"AutomationPortal","FileFullPath":"D:\\\\MSSQL13.MSSQLSERVER\\\\MSSQL\\\\DATA\\\\AutomationPortal.mdf","FileSizeMB":"100.00","SpaceUsedMB":"10.25","MaxfileSizeMB":"-0.01","SPaceOnVolumeMB":"95110.38","AutogrowSetting":"8192"},{"DatabaseID":"21","fileid":"3","databaseName":"AutomationPortal","FileLogicalName":"AutomatioPortal_01","FileFullPath":"D:\\\\MSSQL13.MSSQLSERVER\\\\MSSQL\\\\DATA\\\\AutomatioPortal_01.ndf","FileSizeMB":"100.00","SpaceUsedMB":"0.06","MaxfileSizeMB":"130.00","SPaceOnVolumeMB":"95110.38","AutogrowSetting":"8192"}]}}'
var data = JSON.parse(value);
var tablearray = [];
tablearray.push("<table><tr>")
var queryRow = data.root.row.length;
var headerProperty = Object.keys(data.root.row[0]);
for (i=0;i<headerProperty.length;i++){
tablearray.push("<th>"+headerProperty[i]+"</th>");
}
tablearray.push("</tr>");
//console.log(tablearray);
for (i=0;i<queryRow;i++){
tablearray.push("<tr>")
for (j=0;j<headerProperty.length;j++){
// console.log(headerProperty[j]);
// console.log(data.root.row[0].DatabaseID);
// console.log(data.root.row[i].headerProperty[j]);
tablearray.push("<td>"+data.root.row[i].headerProperty[j]+"</td>");
}
tablearray.push("</tr>");
}
tablearray.push("</table>");
tablearray.join('');
When I run the above script it gives me the following error, I am unable to fix the issue.
tablearray.push(""+data.root.row[i].headerProperty[j]+""); ^
TypeError: Cannot read property '0' of undefined at Object. (C:\Users\convertjsontohtml.js:21:55) at Module._compile (internal/modules/cjs/loader.js:678:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10) at Module.load (internal/modules/cjs/loader.js:589:32) at tryModuleLoad (internal/modules/cjs/loader.js:528:12) at Function.Module._load (internal/modules/cjs/loader.js:520:3) at Function.Module.runMain (internal/modules/cjs/loader.js:719:10) at startup (internal/bootstrap/node.js:228:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)
The Output I am expecting is like ""