Skip to content

Commit c76c968

Browse files
authored
Merge pull request #676 from Nikunj-bisht/csv-to-json/tool
Added csv to json converter tool
2 parents 5e2957e + a28a011 commit c76c968

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
## Project Information
3+
4+
This is a small project in which a csv file of any size data is being converted into JSON format and all this task is being done in the worker thread not on the main thread you can look into the code for more details
5+
6+
7+
8+
## Use
9+
- Open index.html with live server
10+
11+
- Select file by clicking on Choose file button and the result will be displayed in your page.Make sure the file is not corrupted
12+
13+
14+
### Tested Platform
15+
- chrome
16+
17+
- FireFox
18+
19+
### Screenshot
20+
21+
![Screenshot from 2023-10-06 03-26-25](https://github.com/Nikunj-bisht/javascript-projects/assets/52692588/f942b4ae-4393-4182-91f8-392b403ea931)
22+
23+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
onmessage = (mssg) => {
2+
let f = mssg.data;
3+
let fs = new FileReader();
4+
fs.readAsBinaryString(f);
5+
let jsonOutput = [];
6+
let headers = [];
7+
fs.onloadend = function (e) {
8+
const rows = e.target.result.split("\r\n");
9+
for (let i = 0; i < rows.length; i++) {
10+
var cells = rows[i].split(",");
11+
let obj = {};
12+
13+
for (let j = 0; j < cells.length; j++) {
14+
if (i === 0) {
15+
headers.push(cells[j]);
16+
} else {
17+
obj[headers[j]] = cells[j];
18+
}
19+
}
20+
jsonOutput.push(obj);
21+
}
22+
postMessage(JSON.stringify(jsonOutput));
23+
};
24+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<input type="file" id="fileP" />
4+
<h1 id="rem"></h1>
5+
</body>
6+
<script src="./main.js"></script>
7+
</html>

CsvToJsonConverter/nikunj/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fileWorker = new Worker("fileWorker.js");
2+
fileWorker.onmessage = (mssg) => {
3+
document.getElementById("rem").innerHTML = mssg.data;
4+
};
5+
document.getElementById("fileP").addEventListener("change", function () {
6+
let re = new FileReader();
7+
re.onload = function () {
8+
fileWorker.postMessage(document.getElementById("fileP").files[0]);
9+
};
10+
re.readAsText(this.files[0]);
11+
});

0 commit comments

Comments
 (0)