I'm am executing a command in a go program which gives me some output with the structure
c := exec.Command("mycommand")
stdout, _ := c.Output()
Output
{
'name': 'mike',
'phone': '12345'
},
{
'name': 'jim',
'phone': '1234'
}, //notice the final comma
To make valid json, I am trying to prepend [
and then append ]
, and, finally, remove the final comma.
k := "["
mystring := string(stdout)
k += mystring
k += "]"
str := strings.Replace(k, "},]", "}]", -1)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, str)
When I put the final product in a json validator, I get this error
Error: Parse error on line 1:
[{
---^
Expecting 'STRING', '}', got 'undefined'
Question: is there a way to do json.Compact or something similar on a string, or how can I make valid json in this situation?
Update
this is the output that my coding magic produces. I'm not sure exactly what part of it is invalid
[{
'name': 'Leroy',
'phone': '12345'
},
{
'name': 'Jimmy',
'phone': '23456'
}]