title: Using Interfaces with Golang Maps and Structs and JSON
body: <p>I have some JSON code that can look like:</p>
  
  <pre><code>{
      "message_id": "12345",
      "status_type": "ERROR",
      "status": {
          "x-value": "foo1234",
          "y-value": "bar4321"
      }
  }
  </code></pre>
  
  <p>or can look like this. As you can see the "status" element changes from a standard object of strings to an object of array of strings, based on the status_type.  </p>
  
  <pre><code>{
      "message_id": "12345",
      "status_type": "VALID",
      "status": {
          "site-value": [
              "site1",
              "site2"
          ]
      }
  }
  </code></pre>
  
  <p>I am thinking that I need to have my struct for "Status" take a map like "map[string]interface{}", but I am not sure exactly how to do that.</p>
  
  <p>You can see the code here on the playground as well.<br>
  <a href="http://play.golang.org/p/wKowJu_lng" rel="nofollow">http://play.golang.org/p/wKowJu_lng</a></p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
  )
  
  type StatusType struct {
      Id     string            `json:"message_id,omitempty"`
      Status map[string]string `json:"status,omitempty"`
  }
  
  func main() {
      var s StatusType
      s.Id = "12345"
      m := make(map[string]string)
      s.Status = m
      s.Status["x-value"] = "foo1234"
      s.Status["y-value"] = "bar4321"
  
      var data []byte
      data, _ = json.MarshalIndent(s, "", "    ")
  
      fmt.Println(string(data))
  }
  </code></pre>