I have two json-marshallable anonymous structs.
a := struct { Name string `json:"name"` }{"my name"} b := struct { Description string `json:"description"` }{"my description"}
Is there any way to merge them into json to get something like that:
{ "name":"my name", "description":"my description" }
You can embed both structs in another.
type name struct { Name string `json:"name"` } type description struct { Description string `json:"description"` } type combined struct { name description }
The JSON package will treat embedded structs kind of like unions, but this can get clunky pretty quickly.
It's a bit convoluted but I suppose you could do something like this:
a := struct { Name string `json:"name"` }{"my name"} b := struct { Description string `json:"description"` }{"my description"} var m map[string]string ja, _ := json.Marshal(a) json.Unmarshal(ja, &m) jb, _ := json.Marshal(b) json.Unmarshal(jb, &m) jm, _ := json.Marshal(m) fmt.Println(string(jm))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With