In Go, how can we convert snake_case keys in a JSON to camelCase ones recursively?
I am writing one http api in Go. This api fetches data from datastore, does some computation and returns the response as JSON.
The situation is that the JSON document in the datastore (ElasticSearch) is present with snake_case keys while the API response should be camelCase-based (this is just to align with other api standards within the project). The source which inserts into ES can't be modified. So it's only at the api level that the key conversion has to take place.
I have written a struct which is reading JSON from datastore nicely. But how can I convert the keys to camelCase in Go?
The JSON can be nested and all keys have to be converted. The JSON is arbitrarily large; i.e. some keys are just mapped to type interface{}
I am also using Go's echo framework for writing the api.
Ex.
{
"is_modified" : true,
{ "attribute":
{
"legacy_id" : 12345
}
}
}
TO
{
"isModified" : true,
{ "attribute":
{
"legacyId" : 12345
}
}
}
Any pointers on how to do this in Go?
Struct:
type data_in_es struct {
IsModified bool `json:"is_modified,omitempty"`
Attribute *attribute `json:"attribute,omitempty"`
}
type attribute struct {
LegacyId int `json:"legacy_id,omitempty"`
}