Is there any possibility in Golang to unmarshal a JSON object into a struct in which the JSON object has an attribute that can be either object or array (or generally supports various types) in different cases?
for example in one case JSON may look like this:
{
"config": {
"source": "config.cnf"
}
}
but in the same time, the JSON may be like this as well:
{
"config": [ "value1", "value2" ]
}
if so, how would the struct look like?
You should decode to an empty interface (interface{}
). Because it has no methods, every type implements it.
type Data struct {
Config interface{}
}
I have created an elaborate Playground demonstrating this.