I'm consuming an endpoint (which I don't own and I cannot fix) and this endpoint returns JSON.
The problem is this JSON can come in different formats:
Format 1:
{
"message": "Message"
}
or
{
"message": ["ERROR_CODE"]
}
Depending on what happened.
I'd like to have one struct to hold this response so later I can check if the message
is a string or array, and properly follow a flow.
Is it possible to do it in Go? The first approach I thought was to have two structs and try to decode to the one with string
, and if an error happens, try to decode to the one with array
.
Is there a more elegant approach?