Convert CSV to automatically nested JSON in Golang.
This project transforms CSV to JSON without struct and allows recreating nested object using delimiters.
The Reason for this tool is I looked for golang examples but found most of it using struct, so I built this to be flexible enough for any file.
- After Downloading the go file you can run
go run main.go -path={path to csv file} > {path to output json file}
Examplego run main.go -path=/tmp/data.csv > /tmp/output.json
- you will have a new file in the output location with JSON extension.
If I have a csv file contains a test data test.csv
:
Id,Name,Age
1,Aditya,26
2,Raj,22
and we need to convert it to json file:
go run main.go -path=/tmp/test.csv > /tmp/output.json
after writing this command you will get another file in the tmp
directory called output.json
with the data in the new format:
[
{
"Age":"26",
"Id":"1",
"Name":"Aditya"
},
{
"Age":"22",
"Id":"2",
"Name":"Raj"
}
]
and that's it for basic transformation.
If I have a csv file contains a test data with employee information as internal object test.csv
:
id,employee.name,employee.age
1,Aditya,26
2,Raj,22
and we need to convert it to json file:
go run main.go -path=/tmp/test.csv > /tmp/output.json
after writing this command you will get another file in the tmp
directory called output.json
with the data in the new format:
[
{
"employee":{
"age":"26",
"name":"Aditya"
},
"id":"1"
},
{
"employee":{
"age":"22",
"name":"Raj"
},
"id":"2"
}
]
If I have a csv file contains a test data with info as array test.csv
:
id,employee.name,employee.age,employee.info[0],employee.info[1],employee.info[2]
1,Aditya,26,--AdditionalInfo1--,--AdditionalInfo2--,--AdditionalInfo3--
2,Raj,22,--Info1--,--Info2--,--Info3--
and we need to convert it to json file:
go run main.go -path=/tmp/test.csv > /tmp/output.json
after writing this command you will get another file in the tmp
directory called output.json
with the data in the new format:
[
{
"employee":{
"age":"26",
"info":[
"--AdditionalInfo1--",
"--AdditionalInfo2--",
"--AdditionalInfo3--"
],
"name":"Aditya"
},
"id":"1"
},
{
"employee":{
"age":"22",
"info":[
"--Info1--",
"--Info2--",
"--Info3--"
],
"name":"Raj"
},
"id":"2"
}
]
If I have a csv file contains a test data with multi-level object test.csv
:
id,employee.name.firstname,employee.name.lastname,employee.age,employee.info[0],employee.info[1],employee.info[2]
1,Aditya,Raj,26,--AdditionalInfo1--,--AdditionalInfo2--,--AdditionalInfo3--
2,Raj,Aditya,22,--Info1--,--Info2--,--Info3--
and we need to convert it to json file:
go run main.go -path=/tmp/test.csv > /tmp/output.json
after writing this command you will get another file in the tmp
directory called output.json
with the data in the new format:
[
{
"employee":{
"age":"26",
"info":[
"--AdditionalInfo1--",
"--AdditionalInfo2--",
"--AdditionalInfo3--"
],
"name":{
"firstname":"Aditya",
"lastname":"Raj"
}
},
"id":"1"
},
{
"employee":{
"age":"22",
"info":[
"--Info1--",
"--Info2--",
"--Info3--"
],
"name":{
"firstname":"Raj",
"lastname":"Aditya"
}
},
"id":"2"
}
]
If I have a csv file contains a test data with multi-level object with arrays test.csv
:
id,employee.name.firstname,employee.name.lastname,employee.age,employee.info[0].title,employee.info[0].content,employee.info[1].title,employee.info[1].content
1,Aditya,Raj,26,--AdditionalInfoTitle1--,InfoContent1,--AdditionalInfo2--,InfoContent2
2,Raj,Aditya,22,--Info1--,--Content1--,--Info2--,--Content2--
and we need to convert it to json file:
go run main.go -path=/tmp/test.csv > /tmp/output.json
after writing this command you will get another file in the tmp
directory called output.json
with the data in the new format:
[
{
"employee":{
"age":"26",
"info":[
{
"content":"InfoContent1",
"title":"--AdditionalInfoTitle1--"
},
{
"content":"InfoContent2",
"title":"--AdditionalInfo2--"
}
],
"name":{
"firstname":"Aditya",
"lastname":"Raj"
}
},
"id":"1"
},
{
"employee":{
"age":"22",
"info":[
{
"content":"--Content1--",
"title":"--Info1--"
},
{
"content":"--Content2--",
"title":"--Info2--"
}
],
"name":{
"firstname":"Raj",
"lastname":"Aditya"
}
},
"id":"2"
}
]