I need to Unmarshal json from a 3rd party API. Although I know the response type I need to make sure I don't loose any field that the API may introduce (the api has no documentation) so I'm wondering how can I do that. Ideally I would like to store the unknown fields in an interface{} value and perhaps encode it for later audit. Here is what i've tried so far but it doesn't work(Data
field which is "unknown" at compile time is lost during unmarshalling).
package main
import (
"encoding/json"
"fmt"
)
type Tweet struct {
User_id int
Message string
Unknown
}
type Unknown map[interface{}]interface{}
func main() {
// Define an empty interface
var t Tweet
// Convert JSON string into bytes
b := []byte(`{"user_id": 1, "message": "Hello world", "Date": "somerandom date"}`)
// Decode bytes b into interface i
json.Unmarshal(b, &t)
fmt.Println(t)
}