title: Unmarshal JSON Array of arrays in Go
body: <p>I want to parse some json data in go. The data looks like this:</p>
  
  <blockquote>
    <p>{"id":"someId","key_1":"value_1","key_2":"value_2","key_3":"value_3","points":[[1487100466412,"50.032178","8.526018",300,0.0,26,0],[1487100471563,"50.030869","8.525949",300,0.0,38,0],[1487100475722,"50.028514","8.525959",225,0.0,69,-900],[1487100480834,"50.025827","8.525793",275,0.0,92,-262],...]}</p>
  </blockquote>
  
  <p>I built a go struct:</p>
  
  <pre><code>type SomeStruct struct {
     ID   string `json:"id"`
     Key1 string `json:"key_1"`
     Key2 string `json:"key_2"`
     Key3 string `json:"key_3"`
     Points []Point `json:"points"`
  }
  
  type Point struct {
     Timestamp int64 `json:"0"`
     Latitude float64 `json:"1,string"`
     Longitude float64 `json:"2,string"`
     Altitude int `json:"3"` 
     Value1 float64 `json:"4"`
     Value2 int `json:"5"`
     Value3 int `json:"6"`      
  }
  </code></pre>
  
  <p>I unmarshal the json data</p>
  
  <pre><code>var track SomeStruct
  error := json.Unmarshal(data,&amp;track)
  if(error != nil){
      fmt.Printf("Error while parsing data: %s", error)
  }
  </code></pre>
  
  <blockquote>
    <p>json: cannot unmarshal array into Go value of type Point{someId value_1 value_2 value_3 [{0 0 0 0 0 0 0} {0 0 0 0 0 0 0} {0 0 0 0 0 0 0}...]}</p>
  </blockquote>
  
  <p>So the first json keys are parsed correctly, but I cannot figure out how to get the point data, which is an array of arrays.</p>
  
  <p>The generate struct is also the suggest one from here, except I don't use a nested struct but a separate type. Using the suggested nested struct does not make a difference:
  <a href="https://mholt.github.io/json-to-go/" rel="noreferrer">JSON-to-Go</a></p>
  
  <p>Do I need to implement my own Unmarshaller for this?</p>
  
  <p>======= UPDATE SOLUTION ============</p>
  
  <p>It is enough to implement the UnmarshalJSON interface for the Point struct.
  The example below does not contain proper error handling but it show the direction.</p>
  
  <p><a href="https://play.golang.org/p/O9-JlEAKcq" rel="noreferrer">Playground example</a></p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "strconv"
  )
  
  type SomeStruct struct {
      ID     string  `json:"id"`
      Key1   string  `json:"key_1"`
      Key2   string  `json:"key_2"`
      Key3   string  `json:"key_3"`
      Points []Point `json:"points"`
  }
  
  type Point struct {
      Timestamp int64
      Latitude  float64
      Longitude float64
      Altitude  int
      Value1    float64
      Value2    int16
      Value3    int16
  }
  
  func (tp *Point) UnmarshalJSON(data []byte) error {
      var v []interface{}
      if err := json.Unmarshal(data, &amp;v); err != nil {
          fmt.Printf("Error whilde decoding %v\n", err)
          return err
      }
      tp.Timestamp = int64(v[0].(float64))
      tp.Latitude, _ = strconv.ParseFloat(v[1].(string), 64)
      tp.Longitude, _ = strconv.ParseFloat(v[2].(string), 64)
      tp.Altitude = int(v[3].(float64))
      tp.Value1 = v[4].(float64)
      tp.Value2 = int16(v[5].(float64))
      tp.Value3 = int16(v[6].(float64))
  
      return nil
  }
  
  func main() {
  
      const data =    `{"id":"someId","key_1":"value_1","key_2":"value_2","key_3":"value_3","points":[[1487100466412,"50.032178","8.526018",300,0.0,26,0],[1487100471563,"50.030869","8.525949",300,0.0,38,0],[1487100475722,"50.028514","8.525959",225,0.0,69,-900],[1487100480834,"50.025827","8.525793",275,0.0,92,-262]]}`
  
  var something SomeStruct
  json.Unmarshal([]byte(data), &amp;something)
  
  fmt.Printf("%v", something)
  }
  </code></pre>