Go: csv-to-json
Convert CSV to (nested) JSON.
Table of Contents
Usage
package main
import (
"fmt"
"github.com/thorborn-dev/go-csv-to-json"
)
func main() {
csv, err := csv.ReadCSV("path/to/csv")
if err != nil {
panic(err)
}
json, err := csv.ToJSON()
if err != nil {
panic(err)
}
fmt.Println(json)
}
Examples
Basic Data Types
CSV Input:
name,age,registered,verified
John,25,TRUE,false
JSON Output:
[
{
"name": "John",
"age": 25,
"registered": true,
"verified": false
}
]
Objects
CSV Input:
user.firstname,user.lastname
John,Doe
JSON Output:
[
{
"user": {
"firstname": "John",
"lastname": "Doe"
}
}
]
Arrays
Arrays of Basic Data Types
CSV Input:
names[0],names[1]
John,Jane
JSON Output:
[
{
"names": ["John", "Jane"]
}
]
Arrays of Objects
CSV Input:
users[0].firstname,users[0].lastname,users[1].firstname,users[1].lastname
John,Doe,Jane,Doe
JSON Output:
[
{
"users": [
{
"firstname": "John",
"lastname": "Doe"
},
{
"firstname": "Jane",
"lastname": "Doe"
}
]
}
]
Currently not supported CSV input
Arrays of Arrays
CSV Input:
users[0][0],users[0][1]
John,Jane
Expected JSON Output:
[
{
"users": [["John", "Jane"]]
}
]
License
MIT