0

JAVASCRIPT only please.

  1. Turning Excel file into JSON data
let selectedFile;
// Uploading file
const inputEl = document.getElementById("input");
inputEl.addEventListener("change", function (e) {
  e.preventDefault();
  selectedFile = e.target.files[0];
});
// BTN
const btnEl = document.querySelector("button");
btnEl.addEventListener("click", function () {
  if (selectedFile) {
    let fileReader = new FileReader();
    fileReader.readAsBinaryString(selectedFile);
    fileReader.onload = function (event) {
      // console.log(event.target.result);
      let data = event.target.result;
      let workbook = XLSX.read(data, { type: "binary" });

      workbook.SheetNames.forEach((sheet) => {
        let rowObject = XLSX.utils.sheet_to_row_object_array(
          workbook.Sheets[sheet]
        );
        // console.log(rowObject);
        document.getElementById("jsondata").innerHTML = JSON.stringify(
          rowObject,
          undefined,
          4
        );
      });
    };
  }
});
  1. I get this results after uploading the excel file and clicking in the convert button: json data after converting the excel file

  2. How to save that json data into data.json file in VS code like the example below in the SS I just copy and pasted the json data from the results inside the data.json so I can fetch the data

The main goal is to fetch the JSON data obtained after clicking the covert button

  • Specifically, are you just asking how to write data to a file in JavaScript? How to allow the user to download in-memory data as a file? Something else? – David Nov 14 '22 at 15:21
  • 1
    It's a possible duplicate question, please check this https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server – Alex Berger Nov 14 '22 at 15:21
  • To David: Specifically, I need to fetch the data obtained after converting to json. In order to do that, what do I need to do? Save that data o can i fetch it from the results itself? – guaPeo USA Nov 14 '22 at 16:14

0 Answers0