-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2json.go
More file actions
34 lines (28 loc) · 734 Bytes
/
csv2json.go
File metadata and controls
34 lines (28 loc) · 734 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
package csv2json
import (
"encoding/csv"
"fmt"
"os"
)
// Options to controll the json generation
type Options struct {
LineWiseJSON bool
PrettyPrint bool
QuoteEverything bool
}
// ReadCSV to read the content of CSV File
func ReadCSV(csvFile *os.File, additionalFields map[string]string, opt Options) ([]byte, error) {
defer csvFile.Close()
reader := csv.NewReader(csvFile)
content, err := reader.ReadAll()
if err != nil {
return nil, err
}
if len(content) < 1 {
return nil, fmt.Errorf("Something wrong, the file maybe empty or length of the lines are not the same")
}
if opt.LineWiseJSON {
return lineWiseJSON(content, additionalFields, opt)
}
return intoJSONArray(content, additionalFields, opt)
}