How can I convert CSV to UTF-8?
I have a code that accept file, then it will convert to JSON. The thing is, I need to manually convert the CSV file to UTF-8 first in order to convert it into JSON. I'm using a Japanese characters for the regex.
const Upload = () => {
var fileUpload = document.getElementById('fileUpload')
var summary = document.getElementById('summary')
var json = document.getElementById('json')
if (typeof FileReader !== 'undefined') {
var reader = new FileReader()
summary.innerHTML = ''
json.innerHTML = ''
reader.onload = function (e) {
generateData(e.target.result)
}
reader.readAsText(fileUpload.files[0])
}
}
const generateData = (res) => {
var addressList = []
var regex = //
var results = res.split('\n')
for (var i = 0; i < results.length; i++) {
var address = results[i].replace(/"/g, '').split(',')
if (address.length >= 8) {
addressList.push(address[6] + address[7] + address[8])
}
}
const result = addressList.reduce((accumulatedData, data) => {
const isValid = regex.test(data)
accumulatedData[data] = isValid
return accumulatedData
}, {})
var summary = document.getElementById('summary')
summary.innerHTML = `<label><b>Summary (valid):</b></label> ${
Object.values(result).filter((d) => d).length
} / ${Object.keys(result).length}`
setAddressTestCase(JSON.stringify(result, null, 4))
}
CSV is a data format and UTF-8 is a character encoding.
So, you just need read the file in a correct way.
I found this in Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText
You can set encoding as below:
reader.readAsText(fileUpload.files[0], 'UTF-8')