I have a JSON coming from the Kraken websocket that looks like this:
myJsonString := []byte(`[320,{"as":[["27346.80000","0.48700000","1609084588.716887"],["27350.00000","1.38867881","1609084599.606134"]],"bs":[["27346.70000","0.39439437","1609084613.201520"],["27342.10000","0.08750000","1609084611.036191"]]},"book-10","XBT/USD"]`)
And I'd like to unmarshal to a struct like this:
type OrderBook struct {
ChannelID int
A []OrderBookItem `json:"as"`
B []OrderBookItem `json:"bs"`
Depth string
Pair string
}
type OrderBookItem struct {
Price float64
Volume float64
Time float64
}
I tried with unmarshal into a slice, but it doesn't give the desired result as it just returns a struct with zero's. I read this answer How to parse JSON arrays with two different data types into a struct in Golang, but there the message does not seem to be an array.
func main() {
myJsonString := []byte(`[320,{"as":[["27346.80000","0.48700000","1609084588.716887"],["27350.00000","1.38867881","1609084599.606134"]],"bs":[["27346.70000","0.39439437","1609084613.201520"],["27342.10000","0.08750000","1609084611.036191"]]},"book-10","XBT/USD"]`)
var raw []OrderBook
json.Unmarshal(myJsonString, &raw)
fmt.Println(raw)
}
Result:
[{0 [] [] } {0 [{0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0}] [{0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0} {0 0 0}] } {0 [] [] } {0 [] [] }]
I also tried unmarshaling with interface{} and using switch to access the values, but that seems very cumbersome.
What would be the best practice to unmarshal this into a struct?