The format of my JSON output after I marshal a struct has a lot of escape characters and double quotes.
I've tried using an encoder, Marshalling, RawMessages, forcefully removing parts of the string.
data := ChannelData{}
if err := rows.Scan(&data.Idx, &data.MciIdx, &data.Channel, &data.MatchIdx, &data.MatchCx, &data.StartTs, &data.EndTs, &data.Len, &data.MatchStartTs, &data.MatchEndTs, &data.MatchLen, &data.Happened, &data.Instance); err != nil {
log.Printf("%+v
", rows)
log.Error().Err(err).Msg("unable to scan mysql data into struct")
continue
}
jsonD, err := json.Marshal(&data)
if err != nil {
log.Error().Err(err).Msg("cannot marshal data")
}
log.Debug().Msg(string(jsonD))
type ChannelData struct {
Idx string `json:"idx,string"`
MciIdx string `json:"mci_idx,string"`
Channel string `json:"channel,string"`
MatchIdx string `json:"match_idx,string"`
MatchCx string `json:"match_cx,string"`
StartTs string `json:"start_ts,string"`
EndTs string `json:"end_ts,string"`
Len string `json:"len,string"`
MatchStartTs string `json:"match_start_ts,string"`
MatchEndTs string `json:"match_end_ts,string"`
MatchLen string `json:"match_len,string"`
Happened string `json:"happened,string"`
Instance string `json:"instance,string"`
}
I am getting:
{"level":"debug","time":"2019-07-10T20:12:09Z","message":"{\"idx\":\"\\"8931741865\\"\",\"mci_idx\":\"\\"107265043\\"\",\"channel\":\"\\"WPVIDT\\"\",\"match_idx\":\"\\"36028797060807935\\"\",\"match_cx\":\"\\"\\"\",\"start_ts\":\"\\"2019-07-10 17:57:59\\"\",\"end_ts\":\"\\"2019-07-10 17:58:14\\"\",\"len\":\"\\"00:00:15\\"\",\"match_start_ts\":\"\\"2019-06-05 07:14:52\\"\",\"match_end_ts\":\"\\"2019-06-05 07:15:08\\"\",\"match_len\":\"\\"00:00:16\\"\",\"happened\":\"\\"2019-07-10 17:58:16\\"\",\"instance\":\"\\"172.17.65.80\\"\"}"}
I expect it to be in JSON format:
{"level":"debug","time":"2019-07-10T20:12:09Z","message":"{"idx":"8931741865","mci_idx":"107265043","channel":"WPVIDT"...}"}
For removing double quotes you could use strconc.Unquote
It would be great to see an example of a row of data to see if anything else is missing.
But from what is available here, if there is quotes in the string json.Marshal
will not automagically remove them. You can remove quotes using the above command.
Look at This Example for Unquote
.
You can probably just write one method on the ChannelData
struct that will unquote each field.
func (c *ChannelData) unquote() {
c.Idx = strconv.Unquote(c.Idx)
.
.
.
}