7

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"`
}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
badjan
  • 95
  • 1
  • 1
  • 8
  • Just modify your json struct tags. https://goplay.space/#PQxts3yhLi – reticentroot Oct 27 '17 at 16:18
  • @reticentroot I think the issue is that he needs to preserve his current JSON struct tags for unmarshalling the JSON coming from the datasource and then needs to marshall the same data with new JSON tags in which case he'd need to do some custom marshalling. – Conor Oct 27 '17 at 16:23
  • 1
    Then create two structs. One for unmarshaling snake_case and cast to the one that marshaling CamelCase – reticentroot Oct 27 '17 at 16:24
  • Great question +1. The preferred Go term is MixedCaps or mixedCaps not CamelCase. See https://golang.org/doc/effective_go.html#mixed-caps. –  Oct 11 '20 at 06:08
  • @badjan could you please share with us information about how you solved this problem? – Nurzhan Nogerbek Apr 01 '21 at 17:45

1 Answers1

20

Since Go 1.8 you can define two structs that only differ in their tags and trivially convert between the two:

package main

import (
    "encoding/json"
    "fmt"
)

type ESModel struct {
    AB string `json:"a_b"`
}

type APIModel struct {
    AB string `json:"aB"`
}

func main() {
    b := []byte(`{
            "a_b": "c"
    }`)

    var x ESModel
    json.Unmarshal(b, &x)

    b, _ = json.MarshalIndent(APIModel(x), "", "  ")
    fmt.Println(string(b))
}

https://play.golang.org/p/dcBkkX9zQR

To do this in general, attempt to unmarshal the JSON document into a map. If it succeeds, fix all the keys and recursively call your function for each value in the map. The example below shows how to convert all keys to upper case. Replace fixKey with the snake_case conversion function.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "strings"
)

func main() {
    // Document source as returned by Elasticsearch
    b := json.RawMessage(`{
            "a_b": "c",
            "d_e": ["d"],
            "e_f": {
                    "g_h": {
                            "i_j": "k",
                            "l_m": {}
                    }
            }
    }`)

    x := convertKeys(b)

    buf := &bytes.Buffer{}
    json.Indent(buf, []byte(x), "", "  ")
    fmt.Println(buf.String())
}

func convertKeys(j json.RawMessage) json.RawMessage {
    m := make(map[string]json.RawMessage)
    if err := json.Unmarshal([]byte(j), &m); err != nil {
            // Not a JSON object
            return j
    }

    for k, v := range m {
            fixed := fixKey(k)
            delete(m, k)
            m[fixed] = convertKeys(v)
    }

    b, err := json.Marshal(m)
    if err != nil {
            return j
    }

    return json.RawMessage(b)
}

func fixKey(key string) string {
    return strings.ToUpper(key)
}

https://play.golang.org/p/QQZPMGKrlg

Peter
  • 29,454
  • 5
  • 48
  • 60