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:[email protected]
]
(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?
Use github.com/json-iterator/go package instead of encode.json. It can encode map[interface{}]interface{} to JSON.
https://github.com/json-iterator/go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With