title: Golang map json to struct
body: <p>I have a JSON which I need to extract the data out of it using a struct:</p>
  
  <p>I am trying to map it to the below struct:</p>
  
  <pre><code>type Message struct {
      Name   string `json:"name"`
      Values []struct {
          Value int `json:"value,omitempty"`
          Comments int `json:"comments,omitempty"`
          Likes    int `json:"likes,omitempty"`
          Shares   int `json:"shares,omitempty"`
      } `json:"values"`
  }
  </code></pre>
  
  <p>This is my json:</p>
  
  <pre><code>[{
          "name": "organic_impressions_unique",
          "values": [{
              "value": 8288
          }]
      }, {
          "name": "post_story_actions_by_type",
          "values": [{
              "shares": 234,
              "comments": 838,
              "likes": 8768
          }]
      }]
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>How to structure my struct?</li>
  <li>How to read the name, values and comments?</li>
  </ol>
  
  <p>So far I couldn't read the data using the below code:</p>
  
  <pre><code>msg := []Message{}
  getJson("https://json.url", msg)
  println(msg[0])
  </code></pre>
  
  <p>the getJson function:</p>
  
  <pre><code>func getJson(url string, target interface{}) error {
      r, err := myClient.Get(url)
      if err != nil {
          return err
      }
      defer r.Body.Close()
  
      return json.NewDecoder(r.Body).Decode(target)
  }
  </code></pre>