text: const fs = require("fs");
  
  let csvFile;
  
  try {
    csvFile = fs.readFileSync("electric-guitars.csv", "utf-8");
  } catch (err) {
    console.log(err);
    return;
  }
  
  function splitCsv(str) {
    return str.split(",").reduce(
      (acc, curr) => {
        if (acc.isConcatting) {
          acc.soFar[acc.soFar.length - 1] += "," + curr;
        } else {
          acc.soFar.push(curr);
        }
        if (curr.split('"').length % 2 == 0) {
          acc.isConcatting = !acc.isConcatting;
        }
        return acc;
      },
      { soFar: [], isConcatting: false }
    ).soFar;
  }
  
  const lines = csvFile.split("\n").filter((line) => line.trim() !== "");
  
  const keys = lines[0].split(",");
  
  const guitarsById = {};
  
  const guitarPlayerKeys = [
    "guitarplayerid",
    "guitarplayerfirstname",
    "guitarplayerlastname",
  ];
  
  for (let i = 1; i < lines.length; i++) {
    const values = splitCsv(lines[i]);
    const guitarId = values[0];
    if (!guitarsById[guitarId]) {
      guitarsById[guitarId] = {
        guitarplayers: [],
      };
    }
  
    const player = {};
  
    for (let j = 0; j < keys.length; j++) {
      if (guitarPlayerKeys.includes(keys[j])) {
        player[keys[j]] = values[j];
        continue;
      }
      guitarsById[guitarId][keys[j]] = values[j];
    }
  
    guitarsById[guitarId].guitarplayers.push(player);
  }
  
  const guitars = Object.values(guitarsById);
  
  fs.writeFileSync("electric-guitars.json", JSON.stringify(guitars));
  