How to convert Excel data into JSON object using JavaScript.

Yamuna Dulanjani
The Startup
Published in
3 min readMay 12, 2019

This will tells you how we can create a JSON object from an uploaded Excel file to the browser.You can achieve this by doing below tasks step by step.

The first thing is getting the excel file from the user. We can do it using the <input> tag in HTML. It shows below. (<input type=”file” id=”fileUploader” name=”fileUploader” accept=”.xls, .xlsx”/>)

Next we have to define what we are going to do when user upload the file. For that we are going to use the change event of the element above.

This is the important part. Now we are going to define what we want to do with the file uploaded. First, we have to read the file uploaded. In here we make an assumption that the user only uploading a one file.

Next we are going to read that file data using a FileReader. From this FileReader we can read the data in our excel file as a binary string. Then we use XLSX which an inbuilt facility of SheetJS js-xlsx to convert our binary string into a JSON object. To use XLSX you have to include the <script lang=”javascript” src=”dist/xlsx.full.min.js”></script>. You can download the xlsx.full.min.js from here. Then include it on your project structure and include that js file into your working file as below.

Now our background work is completed and we can write the code as below.

As a sample, we used an excel like below which only contains name, mobile and email.

sample output for the above excel data.

As above, we can convert the excel data into a JSON object. After that we can use that JSON to do any work we want like as request body, manipulation etc.

For your easiness full code is available on, https://github.com/YDulanjani/ConvertExcelToJSON/tree/master/convertExcelToJSON

--

--