I've been trying to access field from third party api. For example the json response from third party api is like this:
{
"request_id": "bba3b69370774f87bed0e70398a97f45",
"account_id": "2c1cd618",
"number": "6289523686433"
}
I want to get request_id only can I create interface only like this:
type Response struct {
RequestID string json:"request_id"
}
and then do this:
var resp Response
json.Unmarshal(body,&resp)
Is it possible to do this? Or do I need to make all field of response body in Response struct to be able to unmarshall? Thanks
From the json.Unmarshal
docs
By default, object keys which don't have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).
https://golang.org/pkg/encoding/json/#Unmarshal
Here is a runnable example, showing it working as expected