-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcsv2json.js
More file actions
48 lines (38 loc) · 864 Bytes
/
Copy pathcsv2json.js
File metadata and controls
48 lines (38 loc) · 864 Bytes
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
/* eslint-disable no-console, array-callback-return */
const fs = require('node:fs')
const { pluck, uniq } = require('ramda')
const cols = [
'id',
'nom',
'type',
'organisation',
'adresse',
'lat',
'lon',
'url',
'tel',
'mail',
]
function parseCSV(csv) {
const [...lines] = csv.split(';$END$\n')
return lines.map((line, lineId) => {
const obj = {}
const cells = line.split(';')
if (cells.length !== cols.length) {
console.log(`Erreur ligne ${lineId}: ${cells.length}/${cols.length}`)
return
}
cols.forEach((col, i) => {
obj[col] = cells[i].trim() || undefined
})
return obj
}).filter(Boolean)
}
const structures = parseCSV(
fs.readFileSync('data.csv', 'utf-8'),
)
console.log(uniq(pluck('type', structures)))
fs.writeFileSync(
'data.json',
JSON.stringify(structures, null, 2),
)