I'd like to decode a JSON blob into a Go struct, manipulate on it, and encode it back to JSON. However, there are dynamic fields in the JSON that aren't relevant to my struct and I want to maintain them when I serialize back to JSON.
For example:
{ "name": "Joe Smith",
"age": 42,
"phone": "614-555-1212",
"debug": True,
"codeword": "wolf" }
type Person struct {
Name string
Age uint
Phone string
}
var p Person
json.Unmarshal(data, &p)
// Happy birthday
p.Age++
data, _ = json.Marshal(p)
// Any way to maintain the "debug" and "codeword" fields -- which might not
// be known ahead of time?
I know one possibility is to decode everything into a map[string]interface{}
but boy, do things get ugly when you do that.
Is there any way to have the best of both worlds?