How can I call len() on an interface?

JSON parsing with maps in Go uses interfaces everywhere. Imagine you have the following JSON object:

{
    "stuff" : [
        "stuff1",
        "stuff2",
        "stuff3",
    ]
}

The Go JSON library will parse the outer object as a map from keys to values, as you've seen in your code. It maps variable names as keys to the values that correspond to those variable names. However, since it has no way of knowing ahead of time what those values are, the value type of the map is simply interface{}. So let's say you know there's a key called "stuff", and you know that its value is an array. You could do:

arr := myMap["stuff"]

And you know that it's an array type, so you can actually instead do:

arr := myMap["stuff"].([]interface{})

the problem here is that while you're right that it's an array, and the JSON library knows this, it has no way of knowing that every element will be of type string, so there's no way for it to decide that the array type should actually be []string. Imagine if you had done this instead:

{
    "stuff" : [
        "stuff1",
        "stuff2",
        3
    ]
}

Well "stuff" can't now be an array of strings because one of the elements isn't a string. In fact, it can't be an array of anything - there's no single type that would satisfy the types of all of the elements. So that's why the Go JSON library has no choice but to leave it as []interface{}. Luckily, since all you want is the length, you're already done. You can just do:

arr := myMap["stuff"].([]interface{})
l := len(arr)

Now that's all fine and good, but let's say that down the road you want to actually look at one of the elements. You could now take out an element and, knowing that it's a string, do:

arr := myMap["stuff"].([]interface{})
iv := arr[0] // interface value
sv := iv.(string) // string value

NOTE

When I say "array," I mean array in the JSON sense - these are JSON arrays. The data structure that represents them in Go is called a "slice" (Go has arrays too, but they're a separate thing - if you're used to arrays in languages like C or Java, Go slices are the closest analogue).


When dealing with JSON, you can add type declarations for array and object, then add methods as needed to help with conversion:

package main
import "encoding/json"

type (
   array []interface{}
   object map[string]interface{}
)

func (o object) a(s string) array {
   return o[s].([]interface{})
}

func main() {
   data := []byte(`{"matches": []}`)
   var response object
   json.Unmarshal(data, &response)
   matches := response.a("matches")
   mLen := len(matches)
   println(mLen == 0)
}

https://golang.org/ref/spec#Type_declarations

Tags:

Go