-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (100 loc) · 3.09 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// jsonify -- a simple csv to json converter
// Copyright (C) 2024 ravveni
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run jsonify input_file.csv")
return
}
inputFile := os.Args[1]
// Copy input filename as output filename
outputFilename := strings.Split(inputFile, ".")[0]
// Open the CSV file for reading
csvFile, err := os.Open(inputFile)
if err != nil {
fmt.Println("Error opening CSV file:", err)
return
}
defer csvFile.Close()
// Parse the CSV file using the standard library's csv package
reader := csv.NewReader(csvFile)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV file:", err)
return
}
// Use the first row as keys for JSON objects
keys := records[0]
// Convert each row (CSV record) to a map and create an array of maps
var jsonArray []map[string]any
for _, record := range records[1:] { // Skip header row
item := make(map[string]any)
for j, value := range record {
item[keys[j]] = getTypedValue(value)
}
jsonArray = append(jsonArray, item)
}
// Convert the array of maps to JSON format and write it to standard output
outputJson, err := json.MarshalIndent(jsonArray, "", " ")
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
// Create the output file with .json extension
outputFilename = outputFilename + ".json"
outputFile, err := os.Create(outputFilename)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outputFile.Close()
// Write JSON to the output file
_, err = io.WriteString(outputFile, string(outputJson))
if err != nil {
fmt.Println("Error writing to output file:", err)
return
}
fmt.Println("JSON data saved to:", outputFilename)
}
func getTypedValue(value string) any {
intValue, err := strconv.ParseInt(value, 10, 64) // Check if value is an int
if err == nil {
return intValue
}
floatValue, err := strconv.ParseFloat(value, 64) // Check if value is a float
if err == nil {
return floatValue
}
boolValue, err := strconv.ParseBool(value) // Check if value is a bool
if err == nil {
return boolValue
}
cellArray := strings.Split(value, "|")
if len(cellArray) > 1 { // Check if value is an array
cellArrayItems := []any{}
for _, item := range cellArray {
cellArrayItems = append(cellArrayItems, getTypedValue(item))
}
return cellArrayItems
}
return value // Value is a string
}