This question already has an answer here:
I'm receiving a json
object that has a known-static structure inside a key that varies between 10 different values.
Consider lastname
can be any in a list of 10 lastnames:
var lastnames = [...]string { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson" }
Now, this is how the json
looks:
{
(lastname here):
{
"position": value,
"user_email": value
}
}
I tried to unmarshall it using the following structs, but i only get null values:
type Inside struct {
Rol string `json:"position"`
Email string `json:"user_email"`
}
type Outside struct {
Key Inside
}
...
var outside Outside
json.Unmarshal([]byte(body), &outside)
Is it possible to unmarshall this directly without creating 10 different structs? Is there possible workaround?
</div>