Golang : Convert CSV data to JSON format and save to file
Need to load a CSV data file and save it to JSON encoded file or stream it out ... like to a http service ? This tutorial will cover just that :
The Golang code below will first read this data.csv data file :
Adam,36,CEO
Eve,34,CFO
Mike,38,COO
and output to data.json file
[
{"Name":"Adam","Age":36,"Job":"CEO"},
{"Name":"Eve","Age":34,"Job":"CFO"},
{"Name":"Mike","Age":38,"Job":"COO"}
]
csv2json.go
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
)
type Employee struct {
Name string
Age int
Job string
}
func main() {
// read data from CSV file
csvFile, err := os.Open("./data.csv")
if err != nil {
fmt.Println(err)
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
reader.FieldsPerRecord = -1
csvData, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var oneRecord Employee
var allRecords []Employee
for _, each := range csvData {
oneRecord.Name = each[0]
oneRecord.Age, _ = strconv.Atoi(each[1]) // need to cast integer to string
oneRecord.Job = each[2]
allRecords = append(allRecords, oneRecord)
}
jsondata, err := json.Marshal(allRecords) // convert to JSON
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check
// NOTE : You can stream the JSON data to http service as well instead of saving to file
fmt.Println(string(jsondata))
// now write to JSON file
jsonFile, err := os.Create("./data.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
jsonFile.Write(jsondata)
jsonFile.Close()
}
Output :
[{"Name":"Adam","Age":36,"Job":"CEO"},{"Name":"Eve","Age":34,"Job":"CFO"},{"Name":"Mike","Age":38,"Job":"COO"}]
References :
https://www.socketloop.com/references/golang-encoding-json-marshal-function-example
See also : Convert JSON to CSV in Golang
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+33.4k Golang : convert(cast) bytes to string
+8.3k Golang : Progress bar with ∎ character
+23.3k Golang : Fix type interface{} has no field or no methods and type assertions example
+13.1k Generate salted password with OpenSSL example
+8.6k Golang : How to capture return values from goroutines?
+19.7k Swift : Convert (cast) Int to int32 or Uint32
+6.9k Golang : Check if one string(rune) is permutation of another string(rune)
+6.7k Golang : Calculate BMI and risk category
+8.8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+9k Golang : Web(Javascript) to server-side websocket example
+9.3k Golang : interface - when and where to use examples
+18k Golang : Get download file size