-7
rated 0 times
[
0]
[
7]
/ answers: 1 / hits: 27893
/ 11 Years ago, wed, october 9, 2013, 12:00:00
I need to bring in a csv doc and convert it to JSON, so far I have been able to convert it to an array and from the array I'm trying to build a JSON object.
Below is the JavaScript that builds the JSON, but its not in the structure I need, underneath is an example of the structure required.
var jsonObj = []; //declare object
for (var i=1;i<csvAsArray.length;i++) {
jsonObj.push({key: csvAsArray[i][0]}); //key
for (var l=1;l<csvAsArray[0].length;l++) {
jsonObj.push({label: csvAsArray[0][l], values: csvAsArray[i][l]}); //label + value respectively
}
}
Final output required:
{
key: Sample 01,
values: [
{
label : Something ,
value : 1
} ,
{
label : Something ,
value : 2
}
]
},
{
key: Sample 02,
values: [
{
label : Something ,
value : 5
} ,
{
label : Something ,
value : 4
}
]
}
More From » jquery