How to lowercase all field names for MarshalJSON and UnmarshalJSON?

6,413 views
Skip to first unread message

i...@bodokaiser.io

unread,
Jul 5, 2014, 4:36:06 AM7/5/14
to golan...@googlegroups.com
Hello,

I would like to have all field names of a struct lower cased by default. At the moment I define the lowercased field name as json tag:

type FooBar struct {
    Id string `json:"id"`
}

However with bson-, validation-, database- and xml-tags the space is getting very very tight.

Is there some hack how to implement the Unmarshaler Interface with just lowercasing all field names before proceeding?

Bo

tim...@gmail.com

unread,
Jul 5, 2014, 8:41:14 AM7/5/14
to golan...@googlegroups.com, i...@bodokaiser.io
Convert to map[string]interface{} first. Lower case the keys, then encode.

tim...@gmail.com

unread,
Jul 5, 2014, 9:08:44 AM7/5/14
to golan...@googlegroups.com, i...@bodokaiser.io
I did a search on godoc.org. This package looks like it will convert a struct into a map[string]interface{}:

http://godoc.org/github.com/cupcake/sfilter

Sebastien Binet

unread,
Jul 5, 2014, 9:20:51 AM7/5/14
to tim...@gmail.com, golang-nuts, i...@bodokaiser.io
On Sat, Jul 5, 2014 at 3:08 PM, <tim...@gmail.com> wrote:
> I did a search on godoc.org. This package looks like it will convert a
> struct into a map[string]interface{}:
>
> http://godoc.org/github.com/cupcake/sfilter

or:
http://godoc.org/github.com/mitchellh/mapstructure

-s

>
>
>
>
> On Saturday, July 5, 2014 1:41:14 PM UTC+1, tim...@gmail.com wrote:
>>
>> Convert to map[string]interface{} first. Lower case the keys, then encode.
>>
>> On Saturday, July 5, 2014 9:36:06 AM UTC+1, i...@bodokaiser.io wrote:
>>>
>>> Hello,
>>>
>>> I would like to have all field names of a struct lower cased by default.
>>> At the moment I define the lowercased field name as json tag:
>>>
>>> type FooBar struct {
>>> Id string `json:"id"`
>>> }
>>>
>>> However with bson-, validation-, database- and xml-tags the space is
>>> getting very very tight.
>>>
>>> Is there some hack how to implement the Unmarshaler Interface with just
>>> lowercasing all field names before proceeding?
>>>
>>> Bo
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

i...@bodokaiser.io

unread,
Jul 5, 2014, 9:35:33 AM7/5/14
to golan...@googlegroups.com, i...@bodokaiser.io
How do I copy a struct to map[string]interface{} - I would like to avoid third party libraries

C Banning

unread,
Jul 5, 2014, 3:07:43 PM7/5/14
to golan...@googlegroups.com, i...@bodokaiser.io
You don't need `json:"id"` as long as the only difference between the structure member name and the JSON key is capitalization - http://play.golang.org/p/L60PZqbILP

tim...@gmail.com

unread,
Jul 5, 2014, 3:10:42 PM7/5/14
to golan...@googlegroups.com, i...@bodokaiser.io
You would use the reflection API.

Matt Harden

unread,
Jul 5, 2014, 3:36:14 PM7/5/14
to C Banning, golang-nuts, i...@bodokaiser.io
On Sat, Jul 5, 2014 at 2:07 PM, C Banning <clba...@gmail.com> wrote:
You don't need `json:"id"` as long as the only difference between the structure member name and the JSON key is capitalization - http://play.golang.org/p/L60PZqbILP

You don't for Unmarshal, but you do for Marshal.
 

On Saturday, July 5, 2014 3:36:06 AM UTC-5, i...@bodokaiser.io wrote:
Hello,

I would like to have all field names of a struct lower cased by default. At the moment I define the lowercased field name as json tag:

type FooBar struct {
    Id string `json:"id"`
}

However with bson-, validation-, database- and xml-tags the space is getting very very tight.

Is there some hack how to implement the Unmarshaler Interface with just lowercasing all field names before proceeding?

Bo

--

Matt Harden

unread,
Jul 5, 2014, 4:38:44 PM7/5/14
to C Banning, golang-nuts, i...@bodokaiser.io
Here's a quick and dirty way to make your JSON keys lowercase. This will only do so for the top level JSON object.

Dave Cheney

unread,
Jul 7, 2014, 3:22:08 AM7/7/14
to golan...@googlegroups.com, i...@bodokaiser.io


On Saturday, 5 July 2014 18:36:06 UTC+10, i...@bodokaiser.io wrote:
Hello,

I would like to have all field names of a struct lower cased by default. At the moment I define the lowercased field name as json tag:

type FooBar struct {
    Id string `json:"id"`
}

However with bson-, validation-, database- and xml-tags the space is getting very very tight.

Why is space tight ? Do you need to use a smaller font ?

Seriously for moment, tags are the _correct_ solution for this. Apart from a potential aesthetic issue, is there a specific reason why you don't want to use them ?

Alex Buchanan

unread,
Mar 5, 2018, 11:24:47 PM3/5/18
to golang-nuts
I'm bummed there still isn't a good solution for this. It seems like such an easy and extremely common thing. In a decade of webdev, I've possibly never seen a JSON API with Golang style UpperCamelCase keys.

Is there a solution to this that I'm missing? 

Here's one project I'm working on where dozens of types need to have JSON/YAML formats: https://godoc.org/github.com/buchanae/cwl

Every single struct field needs a really obvious duplication of the field name in the tags, just in camel case. There are 3-4 variable name styles that would cover a majority of the data out there: camelCase, snake_case, etc. (Maybe I should do a code crawling exercise and get some numbers on how often tags are an obvious conversion of the field name...)

My documentation and code would rejoice if encoding/json added a callback for formatting the key name.

Can we just add a call to a callback here? https://golang.org/src/encoding/json/encode.go#L1110

Tamás Gulácsi

unread,
Mar 6, 2018, 12:52:04 AM3/6/18
to golang-nuts
You can try github.com/json-iterator/go, it allows several naming strategies (https://github.com/json-iterator/go/blob/master/extra/naming_strategy.go#L9), even custom ones.

Alex Buchanan

unread,
Mar 6, 2018, 1:27:18 AM3/6/18
to golang-nuts
Thanks, that does look helpful, although I'll need to evaluate the tradeoffs of not using the std lib. It's hard to believe a library could be substantially faster than the stdlib without giving something up.

l...@vaultex.net

unread,
Mar 15, 2019, 11:32:01 AM3/15/19
to golang-nuts
Me too I have to add struct tags to over 300 objects, repeating the same text in a different style, totally unproductive and annoying work which the computer should do for me.

In the end I've used a tool that parses the .go files and adds the tags automatically, although that resulted in dozens of structs that don't need to be encoded to have tags too for no reason.

I truly believe the json library should have a setting we can set on the Encoder to automatically format the output in snake/camel/anycase for the api user preference.
Reply all
Reply to author
Forward
0 new messages