将嵌套地图编组到JSON中

I'm trying to marshal this nested map into a JSON string.

map[
  description:Foo Bar
  url:http://foobar.co.uk
  theme_color:#1b1b1b
  markdown:kramdown
  sass:map[
    style:compressed
  ]
  collections:map[
    projects:map[
      output:true
      permalink:/project/:path
    ]
    jobs:map[
      output:true
      permalink:/job/:path
    ]
  ]
  title:Foo Bar
  email:foo@foobarco.uk
]

(Cleaned up output from fmt.Printf("%v", m))

Initially a config file is read and parsed to produce the map, so I don't know the fields in advance, meaning I can't(?) use a struct.

Unmarshalling from YAML into this map of map[string]interface{} works fine, but when I pass this map to json.Marshal, I get the following error.

json: unsupported type: map[interface {}]interface{}

From reading around, I can see that this error is thrown because JSON only supports string keys. What's confusing me, is that the map above doesn't seem to have any non-string keys.

If I remove the nested sass and collections keys, it marshals without any issues.

Is it possible to do some sanity check on the map to confirm that all the keys are infact string and not just interface{} looking like strings?

Most likely, the sub-maps are being created as map[interface{}]interface{} by the YAML parser.

Print out your map with "%#v" instead of "%v" and you will see the types.

Here's an example

package main

import "fmt"

func main() {
    a := map[string]interface{}{
        "A": map[interface{}]interface{}{
            "B": 123,
        },
    }
    fmt.Printf("%#v
",a)
}

Produces:

map[string]interface {}{"A":map[interface {}]interface {}{"B":123}}