-
Notifications
You must be signed in to change notification settings - Fork 0
/
competitionCsvToJson.js
54 lines (45 loc) · 1.28 KB
/
competitionCsvToJson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require('fs');
const readline = require('readline');
const csvToJson = (fileToReadPath,fileToCreatePath) => {
let wyJson = {};
let headersFlag = false;
let headers = null;
const rl = readline.createInterface({
input: fs.createReadStream(fileToReadPath),
crlfDelay: Infinity
});
rl.on('line', (line) => {
line = line.replace(/\"/g,'');
let row = line.split(",");
if(!headersFlag){
headers = row;
headersFlag = true;
} else {
if(row[2] !== 'NULL'){
if(!Array.isArray(wyJson[row[2]])){
wyJson[row[2]] = [];
};
wyJson[row[2]].push({
[headers[0]]: row[0],
[headers[1]]: row[1],
[headers[2]]: row[2]
});
}
}
});
rl.on('close', () => {
writeJson(fileToCreatePath,wyJson);
});
};
const writeJson = (filePath, json) => {
fs.writeFile(
filePath,
JSON.stringify(json),
err => {
if(err) throw err;
console.log('json created!');
}
);
}
csvToJson('wy_competition.csv','wy_competition.json');
csvToJson('tm_competition.csv', 'tm_competition.json');