Reputation: 2072
I have a json file like
[{"sequence": 12188764183, "bids": [["10167.66", "0.91182063"], ["10167.65", "0.016"], ["10164.46", "0.35778977"], ["10164.45", "5.714"], ["10164.01", "0.2"], ["10163.5", "0.01"], ["10163.49", "4.597"], ["10162.19", "0.761"], ["10162.17", "3.999"], ["10162.16", "2.4"], ["10161.93", "0.024"], ["10161.31", "0.00771652"], ["10160.07", "0.27"], ["10160", "3.47279335"], ["10159.99", "0.0011"], ["10159.88", "1.5"], ["10159", "0.20869106"], ["10158.79", "0.04"], ["10158.67", "4.5"], ["10158.03", "0.004"], ["10157.59", "0.725"], ["10156.99", "0.7893"], ["10156.98", "1.6"], ["10155", "0.1"], ["10154.52", "0.29542"], ["10154.15", "2.17"], ["10153.59", "0.3"], ["10151.58", "1.4055"], ["10151.2", "0.123"], ["10150", "0.77685084"], ["10149.99", "0.00104364"], ["10149.69", "1.85"], ["10148.66", "0.19707"], ["10148.65", "0.29554"], ["10148.27", "8.1"], ["10148.17", "0.084409"], ["10146.54", "1.88"], ["10145.74", "0.1721"], ["10145.72", "0.29554"], ["10145", "0.005"], ["10143.94", "1.3"], ["10143.89", "0.003"], ["10142.79", "0.29554"], ["10141.57", "1.6"], ["10141.5", "0.01154149"], ["10141", "0.005"], ["10140", "0.1"], ["10138.88", "1.066"], ["10138.37", "0.07871087"], ["10137.63", "0.79331873"]], "asks": [["10167.67", "0.04447368"], ["10167.72", "0.001"], ["10168.6", "0.2"], ["10168.85", "2"], ["10169.19", "0.29501"], ["10170", "1.19"], ["10170.02", "3.33049225"], ["10172.12", "0.29496"], ["10172.87", "4.5"], ["10173.57", "0.123"], ["10173.64", "4.555"], ["10175.05", "0.29484"], ["10175.18", "4.4"], ["10176.04", "0.55440618"], ["10176.05", "4.1"], ["10176.43", "1.04"], ["10177.98", "0.29478"], ["10178.35", "4.1"], ["10178.37", "0.00463633"], ["10178.79", "1.3"], ["10178.94", "0.01"], ["10179", "0.04"], ["10179.07", "1"], ["10179.82", "0.004"], ["10179.84", "0.001"], ["10180", "1.50558876"], ["10180.02", "0.001"], ["10180.3", "8.1"], ["10180.91", "0.007"], ["10180.92", "0.29482"], ["10181.07", "2.2"], ["10181.67", "0.68359706"], ["10181.71", "0.0015"], ["10182.15", "0.01"], ["10182.41", "0.02"], ["10182.45", "0.004"], ["10182.54", "0.7766"], ["10183.82", "1.9"], ["10183.85", "0.29482"], ["10184", "0.001"], ["10184.31", "0.001"], ["10184.52", "0.0049976"], ["10184.74", "0.00231"], ["10185", "3.17362533"], ["10185.19", "2.5"], ["10185.37", "0.01"], ["10185.68", "1.1"], ["10185.82", "0.002"], ["10185.87", "0.04"], ["10186.41", "0.1721"]], "timestamp": "2020-02-10 00:00:07.665713"},{"sequence": 12188766096, "bids": [["10167.75", "0.2"], ["10167.74", "0.2"], ["10167.72", "5.98300001"], ["10167.71", "0.144"], ["10167.67", "4.1"], ["10167.66", "2.24"], ["10167.65", "0.016"], ["10165.59", "0.35773702"], ["10165.58", "4.036"], ["10164.46", "0.01"], ["10164.45", "5.714"], ["10164.3", "2.2"], ["10163.48", "0.27"], ["10163.47", "4.5"], ["10163.09", "0.024"], ["10162.19", "0.761"], ["10162.17", "3.999"], ["10162.16", "2.4"], ["10161.31", "0.00771652"], ["10161.28", "0.7959"], ["10160.56", "0.04"], ["10160", "3.47279335"], ["10159.99", "0.0011"], ["10159.88", "1.5"], ["10159", "0.20869106"], ["10158.03", "0.004"], ["10157.45", "0.29534"], ["10156.98", "1.6"], ["10156.81", "0.3"], ["10155", "0.1"], ["10154.54", "8.1"], ["10154.52", "0.29542"], ["10154.15", "2.17"], ["10153.59", "0.3"], ["10151.81", "0.197009"], ["10151.58", "1.4055"], ["10150", "0.77685084"], ["10149.99", "0.00104364"], ["10149.69", "1.85"], ["10149", "0.084409"], ["10148.65", "0.29554"], ["10146.54", "1.88"], ["10145.74", "0.1721"], ["10145.72", "0.29554"], ["10145", "0.005"], ["10143.94", "1.3"], ["10143.89", "0.003"], ["10142.79", "0.29554"], ["10141.5", "0.01154149"], ["10141.23", "0.171076"]], "asks": [["10170", "1.18190554"], ["10170.02", "3.33049225"], ["10173.64", "4.555"], ["10174.2", "0.2"], ["10175.05", "0.29484"], ["10175.18", "4.1"], ["10176.04", "0.55440618"], ["10176.05", "4.1"], ["10177.4", "1"], ["10177.46", "1.12603391"], ["10177.67", "4.5"], ["10177.98", "0.29478"], ["10178.35", "4.1"], ["10178.37", "0.001"], ["10178.79", "1.3"], ["10178.94", "0.01"], ["10179", "0.04"], ["10179.21", "0.3"], ["10179.82", "0.004"], ["10179.84", "0.001"], ["10180", "1.50558876"], ["10180.02", "0.001"], ["10180.8", "0.83971639"], ["10180.91", "0.007"], ["10180.92", "0.29482"], ["10181.07", "2.2"], ["10181.67", "0.68359706"], ["10181.71", "0.0015"], ["10182.15", "0.01"], ["10182.41", "0.02"], ["10182.45", "0.004"], ["10183.82", "1.9"], ["10183.85", "0.29482"], ["10184", "0.001"], ["10184.31", "0.001"], ["10184.52", "0.0049976"], ["10184.74", "0.00231"], ["10185", "3.17362533"], ["10185.19", "2.5"], ["10185.37", "0.01"], ["10185.68", "1.1"], ["10185.82", "0.002"], ["10185.87", "0.04"], ["10186.41", "0.1721"], ["10186.54", "0.0015"], ["10186.6", "8.1"], ["10186.78", "0.29482"], ["10186.97", "0.008"], ["10187", "0.15"], ["10187.21", "0.02041222"]], "timestamp": "2020-02-10 00:00:17.702649"},{"sequence": 12188769568, "bids": [["10174.75", "0.016"], ["10174.39", "2"], ["10174.38", "0.2"], ["10170.66", "4.5"], ["10170", "0.35758237"], ["10169.99", "7.825"], ["10169.85", "0.024"], ["10169.61", "1.74"], ["10168.53", "0.88915013"], ["10167.68", "0.40823914"], ["10167.67", "36.80084511"], ["10167.66", "0.036"], ["10167.64", "0.686"], ["10167.5", "0.04"], ["10167.48", "1.8"], ["10166.42", "0.3"], ["10165.58", "4.036"], ["10164.3", "2.2"], ["10163.33", "0.8008"], ["10163.04", "0.579"], ["10162.17", "3.999"], ["10162.16", "2.4"], ["10161.73", "8.1"], ["10161.31", "0.00771652"], ["10160.53", "0.119"], ["10160.39", "0.196843"], ["10160.38", "0.29522"], ["10160", "3.47279335"], ["10159.99", "0.0011"], ["10159.88", "1.5"], ["10159", "0.20869106"], ["10158.03", "0.004"], ["10157.45", "0.29534"], ["10156.98", "1.6"], ["10155", "0.1"], ["10154.52", "0.29542"], ["10154.25", "0.92941369"], ["10154.15", "2.17"], ["10153.1", "1.20082209"], ["10152.11", "0.1668"], ["10151.73", "2.1"], ["10151.58", "0.2955"], ["10150", "0.77685084"], ["10149.99", "0.00104364"], ["10149.97", "0.084409"], ["10149.2", "1.9"], ["10148.65", "0.29554"], ["10148.11", "1.066"], ["10145", "0.005"], ["10144.74", "14.73320213"]], "asks": [["10175.48", "0.46015637"], ["10175.49", "4"], ["10177.35", "6.1"], ["10177.4", "6.1"], ["10178.85", "1.00995404"], ["10178.94", "0.01"], ["10178.97", "0.2"], ["10179", "0.04"], ["10179.09", "0.06477417"], ["10179.82", "0.004"], ["10179.84", "0.001"], ["10180", "1.50558876"], ["10180.02", "0.001"], ["10180.91", "0.007"], ["10181.67", "0.68359706"], ["10181.71", "0.0015"], ["10181.76", "0.00520665"], ["10182.15", "0.01"], ["10182.41", "0.02"], ["10182.45", "0.004"], ["10183.39", "1.83"], ["10183.85", "0.29461"], ["10184", "0.001"], ["10184.31", "0.001"], ["10184.52", "0.0049976"], ["10184.74", "0.00231"], ["10184.87", "4.5"], ["10185", "3.17362533"], ["10185.16", "0.3"], ["10185.19", "2.5"], ["10185.37", "0.01"], ["10185.8", "1.62"], ["10185.82", "0.002"], ["10185.87", "0.04"], ["10186.18", "0.92941369"], ["10186.52", "0.3575008"], ["10186.54", "0.0015"], ["10186.78", "0.29482"], ["10186.97", "0.008"], ["10187", "0.15"], ["10188", "0.02"], ["10188.39", "1.69"], ["10188.4", "0.0049957"], ["10188.59", "0.01"], ["10188.85", "0.3"], ["10189", "0.56"], ["10189.29", "0.001"], ["10189.4", "0.01559953"], ["10189.95", "0.00245026"], ["10190", "0.05726459"]], "timestamp": "2020-02-10 00:00:27.738108"}]
I would like to have the inner bids arrays converted to float32
during unmarshalling. Found a bunch of answers on here. Some involve the use of ",string" tag. Although, it is not clear how to use that for nested slices. I saw an example in golang docs that uses json.RawMessage
to unmarhsal into unmarshaled json. Anyhow, I would really appreciate your pointers here. Right now I have this, which is not what I want
package main
import (
"encoding/json"
"fmt"
)
// DataPointCoinbase - defines the ob coinbase data point
type DataPointCoinbase struct {
Sequence int32 `json:"sequence"`
Bids [][]json.Number `json:"bids"`
Asks [][]json.Number `json:"asks"`
Timestamp string `json:"timestamp"`
}
// ParseCoinbase - parses coinbase orderbook file
func main() {
data := []byte(`[
{
"sequence": 12188764183,
"bids": [
["10167.66", "0.91182063"],
["10167.65", "0.016"],
["10164.46", "0.35778977"]
],
"asks": [
["10167.66", "0.91182063"],
["10167.65", "0.016"],
["10164.46", "0.35778977"]
],
"timestamp": "2020-02-10 00:00:07.665713"}
]`)
var dataPoints []DataPointCoinbase
json.Unmarshal(data, &dataPoints)
fmt.Println(dataPoints)
}
Upvotes: 0
Views: 115
Reputation: 1
One approach would be, you can write helper methods to do the conversions at each depth of the input. The only difference in your case is in addition to doing type assertion, at the endpoint we also have to parse the string. Here are example methods:
package main
import "fmt"
type Slice []interface{}
type Map map[string]interface{}
func (a Slice) A(n int) Slice {
return a[n].([]interface{})
}
func (a Slice) M(n int) Map {
return a[n].(map[string]interface{})
}
func (m Map) A(s string) Slice {
return m[s].([]interface{})
}
func (a Slice) N(n int) float32 {
var f float32
fmt.Sscan(a[n].(string), &f)
return f
}
and example usage:
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
func main() {
o, e := os.Open("a.json")
if e != nil {
log.Fatal(e)
}
a := Slice{}
json.NewDecoder(o).Decode(&a)
n := a.M(0).A("bids").A(0).N(0)
fmt.Println(n == 10167.66)
}
You can write further methods as needed, if your JSON is different.
Upvotes: 1