I have an API that returns a JSON object with a field containing a byte array (simplified):
{
"value": "[208,188,149,77,179,245,29,184]"
}
I can unmarshal a string to a []byte just fine:
var test = make([]byte,0)
testData := []byte("[208,188,149,77,179,245,29,184]")
_ = json.Unmarshal(testData, &test)
fmt.Println(len(test)) // output: 8
However, when I try to unmarshal this via a struct, it doesn't:
type Test struct {
Value []byte `json:"value"`
}
testData := []byte(`
{
"value": "[208,188,149,77,179,245,29,184]"
}
`)
var test = Test{}
err := json.Unmarshal(testData, &test)
if err != nil {
t.Fatal(err) // output: illegal base64 data at input byte 0
}
I get an error:
illegal base64 data at input byte 0
Is there anything I can do besides using json.RawMessage and manually unmarshalling this field separately?
Thanks.
Test.Valuetostring, and do a second unmarshal, this time into a[]byte.