FAQ
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

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Minux at Sep 25, 2013 at 1:53 am

    On Tue, Sep 24, 2013 at 5:12 PM, Magnús Örn Gylfason 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Andrew Gerrand at Sep 25, 2013 at 2:16 am
    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



    On 25 September 2013 07:12, Magnús Örn Gylfason
    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.

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Magnús Örn Gylfason at Sep 25, 2013 at 6:47 pm
    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.
    On Wednesday, September 25, 2013 2:15:30 AM UTC, Andrew Gerrand wrote:

    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



    On 25 September 2013 07:12, Magnús Örn Gylfason <magnus....@gmail.com<javascript:>
    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.

    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matthew Kane at Sep 25, 2013 at 6:52 pm
    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
    wrote:
    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.
    On Wednesday, September 25, 2013 2:15:30 AM UTC, Andrew Gerrand wrote:

    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



    On 25 September 2013 07:12, 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.

    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.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Andy R at Sep 26, 2013 at 12:18 am
    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:
    http://golang.org/pkg/encoding/json/#Marshal

    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

    On Wednesday, September 25, 2013 2:47:36 PM UTC-4, Magnús Örn Gylfason
    wrote:
    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.

    On Wednesday, September 25, 2013 2:15:30 AM UTC, Andrew Gerrand wrote:
    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





    On 25 September 2013 07:12, Magnús Örn Gylfason 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.


    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ingo Oeser at Jul 28, 2014 at 3:26 pm
    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.
    On Monday, July 28, 2014 5:51:29 AM UTC+2, Camilo Aguilar wrote:

    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.
    On Wednesday, September 25, 2013 3:01:03 PM UTC-4, Andy R wrote:

    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:
    http://golang.org/pkg/encoding/json/#Marshal

    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

    On Wednesday, September 25, 2013 2:47:36 PM UTC-4, Magnús Örn Gylfason
    wrote:
    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.

    On Wednesday, September 25, 2013 2:15:30 AM UTC, Andrew Gerrand wrote:
    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





    On 25 September 2013 07:12, 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.


    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.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andy R at Sep 25, 2013 at 3:05 am
    Put quotes around "pushed_at"

    http://play.golang.org/p/PfS1Y6KvmH <http://play.golang.org/p/uyB5J76WxT>
    On Tuesday, September 24, 2013 5:12:55 PM UTC-4, Magnús Örn Gylfason 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.

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 24, '13 at 9:29p
activeJul 28, '14 at 3:26p
posts8
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase