I need to extract data in my ReactJS app from a .csv file. One of the .csv file columns contains a list of strings (it is a geographic coordinate but the backend service which then will process it only accept the coordinate as string). To visualize better, here's some rows of the .csv file:
start,end,min_distance,route_instructions
533973,533952,265.0,"['1.370137,103.909426', '1.372376,103.910224']"
533973,738406,25580.0,"['1.370134,103.909425', '1.367434,103.908488', '1.367082,103.90959', '1.3631,103.905181', '1.367379,103.906715', '1.373975,103.909121', '1.377559,103.911597', '1.381944,103.914456', '1.386583,103.913584', '1.396414,103.848395', '1.420798,103.771168', '1.436768,103.768425', '1.439945,103.768275', '1.439956,103.767668', '1.439202,103.766885', '1.43941,103.767455']"
533973,506906,8656.0,"['1.370137,103.909426', '1.370758,103.909641', '1.370343,103.910834', '1.375625,103.920052', '1.372205,103.930445', '1.375816,103.931883', '1.374337,103.93527', '1.355196,103.963528', '1.352747,103.964389', '1.352858,103.96476']"
I need to parse it into a JSON object so that it looks like this:
{
start: {
0: 533973,
1: 533973,
2: 533973
},
end: {
0: 533952,
1: 738406,
2: 506906
},
min_distance: {
0: 265.0,
1: 25580.0,
2: 8656.0
},
route_instructions: {
0: "['1.370137,103.909426', '1.372376,103.910224']"
1: "['1.370134,103.909425', '1.367434,103.908488', '1.367082,103.90959', '1.3631,103.905181', '1.367379,103.906715', '1.373975,103.909121', '1.377559,103.911597', '1.381944,103.914456', '1.386583,103.913584', '1.396414,103.848395', '1.420798,103.771168', '1.436768,103.768425', '1.439945,103.768275', '1.439956,103.767668', '1.439202,103.766885', '1.43941,103.767455']",
2: "['1.370137,103.909426', '1.370758,103.909641', '1.370343,103.910834', '1.375625,103.920052', '1.372205,103.930445', '1.375816,103.931883', '1.374337,103.93527', '1.355196,103.963528', '1.352747,103.964389', '1.352858,103.96476']"
}
}
I have tried to parse the string using split() method. It worked, but I'm having the problem with the 'route_instructions' column as it stopped parsing (or splitting? I don't know the correct temrs, sorry) after encountering the first comma so for that column the object looks like this:
{
...,
route_instructions: {
0: '"['1.370137',
1: '"['1.370134',
2: '"['1.370137'
}
}
Here's the code I used to parse the .csv file:
const handleCsv = (e) => {
e.preventDefault();
const file = e.target.files[0];
const reader = new FileReader();
reader.readAsText(file);
reader.onload = function () {
const csvText = reader.result;
const formattedData = {};
let rows = csvText.split("\n");
if (rows[rows.length - 1] === "") {
rows = rows.slice(0, -1);
}
const header = rows[0].split(",");
for (let i = 1; i < rows.length; i++) {
const values = rows[i].split(",");
for (let j = 0; j < header.length; j++) {
if (!formattedData[header[j]]) {
formattedData[header[j]] = {};
}
if (
header[j] === "origin_loc" ||
header[j] === "dest_loc" ||
header[j] === "start" ||
header[j] === "end"
) {
formattedData[header[j]][i - 1] = values[j];
} else {
formattedData[header[j]][i - 1] = isNaN(values[j])
? values[j]
: parseFloat(values[j]);
}
}
}
};
};