forked from guilhermemcardoso/go-transform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 1.43 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gotransform <file.csv>")
return
}
csvFile, err := os.Open(os.Args[1])
if err != nil {
fmt.Println("Error opening CSV file:", err)
return
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV file:", err)
return
}
if len(records) < 1 {
fmt.Println("The CSV file is empty or does not have headers.")
return
}
headers := records[0]
var jsonData []map[string]string
for _, row := range records[1:] {
if len(row) != len(headers) {
fmt.Println("Error: number of fields in a row is different.")
return
}
record := make(map[string]string)
for i, header := range headers {
record[header] = row[i]
}
jsonData = append(jsonData, record)
}
jsonOutput, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
fmt.Println("Error while converting to JSON:", err)
return
}
jsonFileName := strings.TrimSuffix(os.Args[1], ".csv") + ".json"
jsonFile, err := os.Create(jsonFileName)
if err != nil {
fmt.Println("Error while creating JSON file:", err)
return
}
defer jsonFile.Close()
_, err = jsonFile.Write(jsonOutput)
if err != nil {
fmt.Println("Error while writing data into JSON file:", err)
return
}
fmt.Printf("JSON file created with success: %s", jsonFileName)
}