I'm trying to convert a json response code, but i keep getting a back a string array instead. The header fields are depending on the table I query, so I cannot hardcode these.
I get a json response like:
{
"records": [
[
1,
"page one",
300
],
[
2,
"page 2",
500
]
],
"header: [
"page_id",
"page_name",
"total"
]
}
But i would like to convert this to
{
[
{
"page_id": 1,
"page_name": "page one",
"total": 300
},
{
"page_id": 2,
"page_name": "page 2",
"total": 500
}
]
}
I tried to create an Array and converting this to json but it still returns a string array, instead of a json array
let array = new Array;
records.forEach((record) => {
const parts = [];
let i = 0;
header.forEach((title) => {
const part = title + ': ' + record[i];
parts.push(part);
i++;
});
array.push(JSON.parse(JSON.stringify(parts)));
});
console.log(array) // Shows a string array?
I would expect
{
[
{
"page_id": 1,
"page_name": "page one",
"total": 300
},
{
"page_id": 2,
"page_name": "page 2",
"total": 500
}
]
}
But actual
[
[ 'page_id: 1', 'page_name: page 1', 'total: 300'],
[ 'page_id: 2', 'page_name: page 2', 'total: 500']
]