Unmarshalling Json with fields with underscores in their names

6,833 views
Skip to first unread message

Magnús Örn Gylfason

unread,
Sep 24, 2013, 5:12:55 PM9/24/13
to golan...@googlegroups.com
Hi

Im parsing a JSON string (the post from GitHubs WebHooks) into a data structure and every json field that has an underscore in the name is returned with zero value. This applies to bools (to false), ints (to 0) and structures (which become uninitialized). Everything else is parsed.

Im using tags (e.g. "`json:pushed_at`") to name all fields.

Is this known behaviour?

cheers,
mg

Andy R

unread,
Sep 24, 2013, 9:08:31 PM9/24/13
to golan...@googlegroups.com
Put quotes around "pushed_at"

minux

unread,
Sep 24, 2013, 9:52:38 PM9/24/13
to Magnús Örn Gylfason, golang-nuts
On Tue, Sep 24, 2013 at 5:12 PM, Magnús Örn Gylfason <magnus....@gmail.com> wrote:
Hi

Im parsing a JSON string (the post from GitHubs WebHooks) into a data structure and every json field that has an underscore in the name is returned with zero value. This applies to bools (to false), ints (to 0) and structures (which become uninitialized). Everything else is parsed.
the "_" character is treated as a lower case character, so those fields are private and json decoder can't
assign to them. 

Im using tags (e.g. "`json:pushed_at`") to name all fields.

Is this known behaviour?

cheers,
mg

--
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/groups/opt_out.

Andrew Gerrand

unread,
Sep 24, 2013, 10:15:30 PM9/24/13
to Magnús Örn Gylfason, golang-nuts
Do your struct field names begin with capital letters? If not, encoding/json can't see them.

Your structs should look something like this:

  type Object struct {
    PushedAt string `json:"pushed_at"`
  }

Andrew



--

Magnús Örn Gylfason

unread,
Sep 25, 2013, 2:47:36 PM9/25/13
to golan...@googlegroups.com, Magnús Örn Gylfason
The way I was doing this was:

PushedAt     int64  `json:pushed_at`

Adding the extra " as you suggested did the trick:

PushedAt     int64  `json:"pushed_at"`

Why this is need for fields with _ I don't understand, e.g. this works:

Size         int64  `json:size`

Seems the " is only needed when a _ is in the json field name.

Thank you both.

Matthew Kane

unread,
Sep 25, 2013, 2:52:33 PM9/25/13
to Magnús Örn Gylfason, golang-nuts
Because the encoding/json package will also consider a case
insensitive match if no tagged struct with an exact match is present.

On Wed, Sep 25, 2013 at 2:47 PM, Magnús Örn Gylfason
--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

Andy R

unread,
Sep 25, 2013, 3:01:03 PM9/25/13
to golan...@googlegroups.com, Magnús Örn Gylfason
You're just lucky that is works without the " for simple names. The reflect package (which is used by json pkg) clearly specifies:


http://golang.org/pkg/reflect/#StructTag

"Each value is quoted using U+0022 '"' characters and Go string literal syntax."

Also the json package states you have to use quotes:

You'll never know what happens in the future, this might break so I'd suggest enclosing all field names with quotes to be safe.

Cheers

Camilo Aguilar

unread,
Jul 27, 2014, 11:51:29 PM7/27/14
to golan...@googlegroups.com, magnus....@gmail.com
I also found that it doesn't like a space between the colon and the double quotes: 

This didn't unmarshal properly for me: `json: "my_name"` 
Changing it to `json:"my_name"` made it work. It is sad the fragility of this.

Ingo Oeser

unread,
Jul 28, 2014, 11:26:44 AM7/28/14
to golan...@googlegroups.com, magnus....@gmail.com
Please note that "go vet" would have found this for you.
So at least you have a chance to catch this issue in a pre-commit hook or CI system before you go into production.
Reply all
Reply to author
Forward
0 new messages