An api endpoint, not within my control, returns json with a key people that can have one of 2 types.
Json:
{
"people":{
"admin":{
"Name": "John Smith",
"Address": "123 main street"
},
"user":{
"Name": "Jane Doe",
"Address": "321 broad street",
}
}
Json type 2:
{
"people":[
{
"name": "Jon Snow",
"pet": "Ghost"
},
{
"name": "Sansa Stark",
"pet": "Lady",
}
]
}
For the first type, I can create a structs as so
type People struct {
Admin *Person `json:"admin,omitempty"`
User *Person `json:"user,omitempty"`
}
type Person struct {
Name string `json:"name,omitempty"`
Address string `json:"adress,omitempty"`
}
or the second type as
type People []*Person
type Person struct{
Name string `json:"name,omitempty"`
Pet string `json:"pet,omitempty"`
}
Is there a way to design a single struct such that I can unmarshal either type of response?