Django API JSONfield fetches an object that I can't use .map to parse and then build a chart
I have a Django serialized API field that contains database JSONfield data that creates a chart (this data is confirmed to work on my Flask backend), but I can't use the same data taken from a Django API
'''
// data.data.linechart: "[{'index': 129, 'Strategy': 28.893}, {'index': 1304, 'Strategy': 33.331}]"
fetch(url)
.then(response => response.json())
.then(data => {
var dataArray = Object.keys(data.data.linechart).map(function(key) {
return [key, data.data.linechart[key]];
});
// Convert the array of key-value pairs to a format that supports map
var mapData = dataArray.map(function(item) {
return { index: item[0], Strategy: item[1] };
});
const series = [
{
name: 'Strategy',
data: mapData
}
];
//Apex Chart
var optionsChart = {
series: [{
name: 'New Data',
data: series
}],
What am I missing here?
Fetch is pulling serialized API data as an object - and the conversion above isn't working. Have others seen the same problem?
I have tried:
- JSON.parse(data),
- JSON.stringify(data),
- Stripping brackets: const dataArray = data2.data.linechart.replace('[', '').replace(']', '');
- changing data.data.linechart to data.data and then isolating linechart = "[{'index': 129, 'Strategy': 28.893}, {'index': 1304, 'Strategy': 33.331}]"
but I cannot convert the chart data JSON from an Object - without splitting the JSON string into individual characters...