Sunday, June 16, 2019

Read and convert csv to json jQuery

Upload .csv File and convert it into json.
This blog is related to how to read uploaded csv file and convert it into array of Json objects with csv file headers as key and data as value.
Lets take an simple example

Html Code:
<input type='file' id='csvFile'></input>
<button type='button' onclick='upload()'>Upload</button>

Jquery:
function upload(){
var input = document.getElementById('csvFile');
file = input.files[0];
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event) {
var csvData = event.target.result;
var items = $.csv.toObjects(event.target.result);
// here we will get details printed in console
console.log(items);
},
reader.onerror = function() {
console.log('Unable to read ' + file.fileName);
};
}

One library(script file) is required to load before uploading file which is
https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js




Read and convert csv to json jQuery

Upload .csv File and convert it into json. This blog is related to how to read uploaded csv file and convert it into array of Json objects...