title: Unmarshalling top-level JSON array in Go
body: <p>I'm learning Go by writing a simple http server and I need to handle some JSON responses.</p>
  
  <p>With an object response, I can unmarshal it idiomatically with 2 lines of code:
      structResult := Foo{}
      json.Unmarshal(structBody, &amp;structResult)</p>
  
  <p>I don't know how to do the same for an array response (see the example below). Is there a way to specify (possibly via json tag) that top-level array should go into a given struct field?</p>
  
  <pre><code>package main
  
  import "fmt"
  import "encoding/json"
  
  type Foo struct {
      Id uint64 `json:"id"`
      Name string `json:"name"`
  }
  
  type BaseResult struct {
      Error string  `json:"error"`
  }
  
  type FooResult struct {
      BaseResult
      Foos []Foo
  }
  
  func main() {
      // Simple and works.
      structBody := []byte(`{"id": 1,"name": "foo"}`)
      structResult := Foo{}
      json.Unmarshal(structBody, &amp;structResult)
      fmt.Printf("%#v\n", structResult)
  
      // Doesn't work.
      arrayBody := []byte(`[{"id": 1,"name": "foo"},{"id": 2,"name": "bar"},{"id": 3,"name": "foobar"}]`) 
      arrayResult := FooResult{}
      json.Unmarshal(arrayBody, &amp;arrayResult)
      fmt.Printf("%#v\n", arrayResult)
  }
  </code></pre>
  
  <p>I know I could make FooResult an array:</p>
  
  <pre><code>type FooResult []Foo
  </code></pre>
  
  <p>but then I lose the ability to specify base object which I would like to use to store error message and such. I also know that I can unmarshal into &amp;fooResult.Foos directly, but I want the code to work with both objects and arrays.</p>
  
  <p>UPDATE</p>
  
  <p>Implementing UnmarshalJSON as suggested by @dyoo partially solves my problem, but I was hoping that I could use BaseResult to store parse error in case JSON has a different structure:</p>
  
  <pre><code>arrayBody := []byte(`{"error": "foo"}`)
  arrayResult := FooResult{}
  json.Unmarshal(arrayBody, &amp;arrayResult)
  fmt.Printf("%#v\n", arrayResult)
  </code></pre>
  
  <p>Of course I could implement more complex logic inside UnmarshalJSON - but isn't there a simpler way to do it?</p>