-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatecsv.js
60 lines (53 loc) · 2.02 KB
/
updatecsv.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
51
52
53
54
55
56
57
58
59
60
const fs = require('fs');
const parse = require('csv-parser');
const csvFilePath = 'rename_tracking.csv';
const jsonFilePath = 'legislativeaffairs_json/legislativeaffairs.json';
const mappings = {};
fs.createReadStream(csvFilePath)
.pipe(parse())
.on('data', (row) => {
const originalFilename = row.originalFilename;
const newFilename = row.newFilename;
mappings[originalFilename] = newFilename;
})
.on('end', () => {
fs.readFile(jsonFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading JSON file:', err);
return;
}
const jsonData = JSON.parse(data);
jsonData.forEach((entry) => {
for (const [originalFilename, newFilename] of Object.entries(mappings)) {
if (entry.reg_link != null) {
entry.reg_link = entry.reg_link.replace(originalFilename, newFilename);
}
if (entry.hearing_report != null) {
entry.hearing_report = entry.hearing_report.replace(originalFilename, newFilename);
}
if (entry['support'] != null) {
entry.support = entry.support.replace(originalFilename, newFilename);
}
if (entry['support.1'] != null) {
entry['support.1'] = entry['support.1'].replace(originalFilename, newFilename);
}
if (entry['support.2'] != null) {
entry['support.2'] = entry['support.2'].replace(originalFilename, newFilename);
}
if (entry['support.3'] != null) {
entry['support.3'] = entry['support.3'].replace(originalFilename, newFilename);
}
if (entry['support.4'] != null) {
entry['support.4'] = entry['support.4'].replace(originalFilename, newFilename);
}
}
});
fs.writeFile(jsonFilePath, JSON.stringify(jsonData, null, 2), (err) => {
if (err) {
console.error('Error writing updated JSON file:', err);
} else {
console.log('JSON file updated successfully.');
}
});
});
});