I'm a newbie to Go and trying use a library from github to parse JSON to CSV. https://github.com/jehiah/json2csv
But i encountered this issue: https://github.com/jehiah/json2csv/issues/22 to which the author is not replying.
I realized that if we give following JSON as json.input to the file:
{"user": {"name":["jehiah, mike, semo"], "password": "root"}, "remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":["jeroenjanssens", "jeroen2", "jero55"], "password": "123"}, "remote_ip": "192.168.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}
{"user": {"name":"markdata", "password": ""}, "remote_ip": "76.216.210.0", "dt" : "[20/Aug/2010:01:12:45 -0400]"}
Now if i try using it as command: go run main.go -k user.name -i input.json -o output.json
it returns following output:
"[jehiah, mike, semo]"
[jeroenjanssens jeroen2 jero55]
markdata
but as described in opend issued, i'm expecting the response as:
jehiah, mike, semo
jeroenjanssens, jeroen2, jero55
markdata
i'm guessing it is happening due to line: https://github.com/jehiah/json2csv/blob/master/main.go#L110 which is anyhow removing commas while reading the line.
Can you please advice to how to achieve above desired output?
Regards
Using this code you will be able to read your json data in a Go struct really easily:
package main
import (
"encoding/json"
"fmt"
)
// define the User type
type User struct {
Name []string
Password string
}
type DataStruct struct {
User User
Remote_ip string
Dt string
}
func main() {
var jsonBlob = []byte(`{"user": {"name":["jehiah, mike, semo"], "password": "root"},
"remote_ip": "127.0.0.1", "dt" : "[20/Aug/2010:01:12:44 -0400]"}`)
var data DataStruct
err := json.Unmarshal(jsonBlob, &data)
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("%+v", data)
}
}
If you have never used the encoding/json package you should read the official Golang json article.
When you have correctly read the data in the DataStruct structure, you will be able to serialize it using the proper Golang csv package.
There are a couple of issues at play here:
The structures defined by JSON are not all representable in CSV, for example:
{"name":"john"}
["john", "mike", "sam"]
There is no CSV standard. Well, there is RFC-4180, but most CSV encoders/decoders don't adhere to it because Microsoft.
The package you are using is very poorly coded.
One of the main differences among CSV implementation is the quote-handling. Consider the JSON string: "\"Hello, world!\""
.
Depending on our marshaller we could end up with any of the following CSV representations for the string:
"""Hello, world!"""
"\"Hello, world!\""
'"Hello, world!"'
Some encoders will even escape the comma inside of the string. The package you describe circumvents this issue by skipping the commas altogether. Which is a very poor design decision if you ask me.
Everything you need is in Go's standard library: