Skip to content

Instantly share code, notes, and snippets.

@mayakraft
Created April 3, 2020 22:50
Show Gist options
  • Save mayakraft/0c26961dffef58b4280512ce6e7d9e7e to your computer and use it in GitHub Desktop.
Save mayakraft/0c26961dffef58b4280512ce6e7d9e7e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="file">
</body>
<script>
// this bit of code opens the file and converts it into a string
document.querySelectorAll("input")[0].onchange = function(event) {
// this is the function that gets called by the browser
var file = event.target.files[0];
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function(readerEvent) {
didLoad(readerEvent.target.result);
}
};
// the file has finished loading, as is converted into one string
function didLoad(text) {
// console.log(text);
// split the string at the newlines
var lines = text.split(/\r\n|\n/);
// lines is an array of strings
// console.log(lines);
// first let's process the categories
var header = lines[0].split(",");
// console.log(header);
var json = [];
for (var i = 1; i < lines.length; i = i + 1) {
var row = lines[i].split(",");
var object = {};
// a second loop loops over the entries in the array
// and puts them into this object
for (var j = 0; j < row.length; j = j + 1) {
var key = header[j];
object[key] = row[j];
}
// the object is now filled up with the contents of the row
json.push(object);
}
// an array of objects
console.log(json);
download(json, "data.json");
};
function download (data, filename) {
var a = document.createElement("a");
var blob = new window.Blob( [ JSON.stringify(data) ] , { type:"text/plain" } );
a.setAttribute("href", window.URL.createObjectURL(blob) );
a.setAttribute("download", filename);
document.body.appendChild(a);
a.click();
a.remove();
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment