I am working on a function for converting a table array to JSON and this is my code. I have checked strJSON is the JSON format I want, but it is a string instead of an JSON object. An error message prompts out saying that "Darren is not defined" when I try to convert it into JSON with "eval". I dont know what is happening.
var myArray=[["Name","Age","Gender"],["Darren",31,"Male"],["Gakki",25,"Female"]];
//expected output
var myJSON=[{"Name":"Darren","Age":31,"Gender":"Male"},{"Name":"Gakki","Age":25,"Gender":"Female"}];
function convertJSON(tableData){
var objJSON=[];
var strJSON=[];
tableData.forEach(function(rowData,i){
if (i==0){
rowData.forEach(function(cellData){
head.push(cellData);
});
}
else{
var objData=[];
var objEntry=[];
rowData.forEach(function(cellData,j){
objData=head[j] + ":" + cellData;
objEntry.push(objData);
});
strJSON.push("{" + objEntry + "}");
}
});
objJSON=eval("[" + strJSON + "]");
return objJSON
}
JSON.parseand you don't even need to do that at this point since strJSON would be JSON already if you did it right.