-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeJson.js
43 lines (39 loc) · 1.51 KB
/
writeJson.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
const constants = require("./constants");
const csv = require('csv-parser');
const fs = require('fs');
var fileType = ".json";
let fileNames = [files[0] + fileType, files[1] + fileType];
let createJson = (_path, _fileName) => {
let resultArr = [], result = {};
fs.createReadStream(_path)
.pipe(csv())
.on('data', (row) => {
let keys = Object.keys(row);
result.province = row[province];
result.region = row[region];
result.lat = row[lat];
result.lon = row[lon];
keys.forEach(key => {
if (key !== province && key !== region && key !== lat && key !== lon) {
result.date = new Date(key);
result.value = row[key];
let resutToSet = {};
resutToSet.province = result.province;
resutToSet.region = result.region;
resutToSet.lat = result.lat;
resutToSet.lon = result.lon;
resutToSet.date = new Date(result.date);
resutToSet.value = result.value;
resultArr.push(resutToSet);
}
});
})
.on('end', () => {
let jsonStr = JSON.stringify(resultArr);
fs.writeFileSync(_fileName, jsonStr);
console.log('Covid file successfully processed');
});
}
for (i = 0; i < paths.length; i++) {
createJson(paths[i], fileNames[i]);
}