go
go copied to clipboard
how do i dynamically change the structs json tag
I'm referencing a struct of third-party library, eg look like:
type User struct {
ID int64 `json:"id"`
Name string `json:"first"` // want to change this to `json:"name"`
tag string `json:"-"`
Another
}
type Another struct {
Address string `json:"address"`
}
I now have the following:
package main
import (
"encoding/json"
"fmt"
"os"
)
type MyUser User
func main() {
anoth := Another{"123 Jennings Street"}
_ = json.NewEncoder(os.Stdout).Encode(
&MyUser {1, "Ken Jennings", "name",
anoth},
)
}
I am trying to json encode the struct but before I do I need to change the json key...eg the final json should look like:
{"id": 1, "name": "Ken Jennings", "address": "123 Jennings Street"}
how can i do it