I'm trying to parse a node package.json file in golang and I've got the following struct:
type packageJson struct {
scripts map[string]interface{} `json:"scripts"`
dependencies map[string]interface{} `json:"dependencies"`
devDependencies map[string]interface{} `json:"devDependencies"`
}
...
var content packageJson
if err := json.Unmarshal(b, &content); err != nil {
return err
}
When I parse the package file however the struct is not being populated (not getting an error though). I suspect it is because the content is an object itself (i.e.: { "scripts":"...", ... }
) and the Unmarshal method wants to convert it into a map[string]interface{}
. Any suggestions how to get around this "issue"? I tried creating a wrapper struct and using jpath
but to no avail. Thanks!
Note: I could do this
var content map[string]interface{}
...
if val, ok := content["scripts"]; !ok { ... }
but I'd like to avoid it if possible.