Q:

Accessing JSON properties when not using struct

I have some JSON and I need to be able to access the properties. As the JSON properties can vary I can't create a struct to unmarshal into.
Example
The JSON could be this:
{"name" : "John Doe", "email" : "john@doe.com"}

or this:
{"town" : "Somewhere", "email" : "john@doe.com"}

or anything else.
How can I access each of the properties?

A:

You can unmarshal it into an interface{}. If you do that, json.Unmarshal will unmarshal a JSON object into a Go map.
For example:
var untypedResult interface{}
err := json.Unmarshal(..., &untypedResult)

result := untypedResult.(map[string]interface{})

// ... now you can iterate over the keys and values of result ...

See <http://blog.golang.org/json-and-go#TOC_5.> for a complete example.

