I am having trouble unmarshalling some JSON in Go. In the below code, I want to be able to extract the value of "c" ("Apple", "Berry") from the JSON (<- main goal/question). What has me particularly confused is when I tried to index into the v
array(?), I get the error: invalid operation: v[0] (type interface {} does not support indexing)
.
This is what brought me to using reflect
and looking at the type of the variable, which as you can see below is "[]interface {}", even if I assert it to be "interface {}". Of note, if I make the line q := interface{}(v)
into q := []interface{}(v)
, I get: cannot convert v (type interface {}) to type []interface {}: need type assertion
. What am I missing? Why does reflect
say it is "[]interface {}" but errors say it is "interface {}"? (<- secondary question / help me understand)
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func main() {
data := []byte("{ \"a\" : { \"b\" : [ { \"n\" : 100, \"c\" : \"Apple\", \"m\" : 1, \"bool\" : false }, { \"n\" : 101, \"c\" : \"Berry\", \"m\" : 2, \"bool\" : false } ] }, \"foo\" : false, \"bar\" : \"foobar\" }")
var a map[string]interface{}
_ = json.Unmarshal(data, &a)
b := a["a"].(map[string]interface{})
for i, v := range b {
fmt.Println(i, reflect.TypeOf(v))
q := interface{}(v)
fmt.Println(q, reflect.TypeOf(q))
}
}
Output:
b []interface {}
[map[m:1 bool:false n:100 c:Apple] map[c:Berry m:2 bool:false n:101]] []interface {}