title: Clean way to conditionally unmarshal JSON to struct
body: <p>I'm sending requests to a JSON API, and it either returns an error...</p>
  
  <pre><code>{
    "error": {
      "code": 404,
      "message": "Document not found.",
      "status": "NOT_FOUND"
    }
  }
  </code></pre>
  
  <p>or the data.</p>
  
  <pre><code>{
    "name": "projectname",
    "fields": {
      "userId": {
        "stringValue": "erw9384rjidfge"
      }
    },
    "createTime": "2018-06-28T00:52:25.638791Z",
    "updateTime": "2018-06-28T00:52:25.638791Z"
  }
  </code></pre>
  
  <p>Here are the corresponding structs</p>
  
  <pre><code>type HttpError struct {
      Code    int    `json:"code"`
      Message string `json:"message"`
      Status  string `json:"status"`
  }
  
  type Document struct {
      Name   string `json:"name"`
      Fields struct {
          UserID struct {
              StringValue string `json:"stringValue"`
          } `json:"userId"`
      } `json:"fields"`
      CreateTime time.Time `json:"createTime"`
      UpdateTime time.Time `json:"updateTime"`
  }
  </code></pre>
  
  <p>Once I get the response, how do I cleanly/concisely unmarshal to the correct struct? I've seen a lot of ugly solutions (maybe Go's fault instead of the writers).</p>
  
  <pre><code>func getDocument() {
      resp, _ := httpClient.Get("example.com")
      defer resp.Body.Close()
      bodyBytes, _ := ioutil.ReadAll(resp.Body)
  
      var data map[string]interface{}
      // How to unmarshal to either HttpError or Document??
      err = json.Unmarshal([]byte(bodyBytes), &amp;data)
  
  }
  </code></pre>
  
  <p>By the way, I can't use the Go Firestore client library because reasons.</p>