I am receiving JSON formatted data from the network, and I need to unmarshal it depending on a key.
Here is the data example:
{
"foo": {
"11883920": {
"fieldA": 123,
"fieldB": [
{
"fieldC": "a",
"fieldD": 1173653.22
}
]
}
},
"bar": {
"123": {
"fieldE": 123
}
}
"anyOtherkey": {...}
}
The logic is that, if the key is foo
it sould be unmarshaled as fooStruct
, and if bar
- as a barStruct
.
What is the best way to implement this logic? (I don't want to unmarshal it to map[string]interface{}
, maybe it is possible with json.NewDecoder()
function but I could not get the expected result).