You can create a custom type, and implement the json.Marshaller interface on that type. That method implementation can transparently do the string -> int conversion:
type IntValueMarshal []map[string]string
func (ivms IntValueMarshal) MarshalJSON() ([]byte, error) {
// create a new map to hold the converted elements
mapSlice := make([]map[string]interface{}, len(ivms))
// range each of the maps
for i, m := range ivms {
intVals := make(map[string]interface{})
// attempt to convert each to an int, if not, just use value
for k, v := range m {
iv, err := strconv.Atoi(v)
if err != nil {
intVals[k] = v
continue
}
intVals[k] = iv
}
mapSlice[i] = intVals
}
// marshal using standard marshaller
return json.Marshal(mapSlice)
}
To use it, something like:
values := []map[string]string{
{"k1": "1", "k2": "somevalue"},
}
json.Marshal(IntValueMarshal(values))