Golang unmarshal json map & array
<p>After reading <a href="http://blog.golang.org/json-and-go" rel="noreferrer">JSON and Go</a>, I do understand the basics of decoding json in Go are. However, there this problem where that input json can be sometimes a map &amp; sometimes an array of maps. </p>

<p>consider the following problem:</p>

<pre><code>package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    b := []byte(`[{"email":"example@test.com"}]`)
    c := []byte(`{"email":"example@test.com"}`)

    var m interface{}

    json.Unmarshal(b, &amp;m)
    switch v := m.(type) {
    case []interface{}:
        fmt.Println("this is b", v)
    default:
        fmt.Println("No type found")
    }

    json.Unmarshal(c, &amp;m)
    switch v := m.(type) {
    case map[string]interface{}:
        fmt.Println("this is c", v)
    default:
        fmt.Println("No type found")
    }

}
</code></pre>

<p>Now, how to I get to the value <code>email</code>: <code>example@test.com</code> in both cases(<code>b</code> &amp; <code>c</code>)</p>

<p><strong>Question:</strong></p>

<ol>
<li>If json is an array, I want to loop over each &amp; print email.</li>
<li>if json is an map, I want to print email directly</li>
</ol>

<p>Play: <a href="http://play.golang.org/p/UPoFxorqWl" rel="noreferrer">http://play.golang.org/p/UPoFxorqWl</a></p>