I'm new to Golang. I have to stringify a map[string]interface{}
in Golang as you would do a JSON.stringify
in javascript. In javascript keys which has their values undefined
gets omitted when you do JSON.stringify
and I want the same thing when I do json.Marshal
in Golang for nil
values. For e.g. in javascript
var activity = {"isvalid": true, "value": {"prop": undefined}, "prop": undefined}
console.log(JSON.stringify(activity))
// Output: {"isvalid":true,"value":{}}
But in Golang if I do this
activity := map[string]interface{}{
"isvalid": true,
"value": map[string]interface{}{"prop": nil},
"prop": nil,
}
jsonStr, _ := json.Marshal(activity)
fmt.Println(string(jsonStr))
// output: {"isvalid":true,"prop":null,"value":{"prop":null}}
https://play.golang.org/p/dvmz32phdNU
There is a difference in output, what is a good way to make the output same in Golang as of Javascript?
EDIT:
The use case involves getting data from various sources which are of type map[string]interface{}
and keys can be arbitrary, so converting it into a struct is not an option for me.