[Vuejs]-Convert CSV file from input tag Vue js to JSON

1👍

You need to await the file content to be loaded using the event target files :

async csvJSON(event) {
          const file = event.target.files.item(0)
           const csv = await file.text();
            console.log(csv)
            var lines = csv.split('\n')

            var result = []

            var headers = lines[0].split(',')

            for (var i = 1; i < lines.length; i++) {
                var obj = {}
                var currentline = lines[i].split(',')

                for (var j = 0; j < headers.length; j++) {
                    obj[headers[j]] = currentline[j]
                }

                result.push(obj)
            }
            console.log('data', result)
        },

Leave a comment