FAQ
Hello,

I'm using json.Marshal to convert a struct to Json, for the purpose of
posting said json to a rest end point.

The struct looks like this;
type Lead struct {
LeadNumber string `json:"Lead_Number__c"`
FirstName string
LastName string
Company string
Email string
Description string
}

After the post, I get the same resource back in the response.

I need to exclude the LeadNumber filed from the POST, as that field is read
only, and assigned by the server. I want to read that same value back into
my struct from the http.Response

How do I exclude a specific field from being converted when using
json.Marshal? Keep in mind, I need to fill the field during json.Unmarshal.


Thanks!
-Jev

--
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

  • Andrew Gerrand at Apr 3, 2013 at 5:15 am
    You'll need to create two structs, I believe.

    Andrew

    On 3 April 2013 15:05, Jev Björsell wrote:

    Hello,

    I'm using json.Marshal to convert a struct to Json, for the purpose of
    posting said json to a rest end point.

    The struct looks like this;
    type Lead struct {
    LeadNumber string `json:"Lead_Number__c"`
    FirstName string
    LastName string
    Company string
    Email string
    Description string
    }

    After the post, I get the same resource back in the response.

    I need to exclude the LeadNumber filed from the POST, as that field is
    read only, and assigned by the server. I want to read that same value back
    into my struct from the http.Response

    How do I exclude a specific field from being converted when using
    json.Marshal? Keep in mind, I need to fill the field during json.Unmarshal.


    Thanks!
    -Jev

    --
    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.
  • Cem Ezberci at Apr 3, 2013 at 5:20 am
    If my understanding is correct, when you marshal the struct, LeadNumber
    will be empty because you wouldn't be posting it with a value there since
    the server is supposed to assign it. However, response will come back with
    LeadNumber populated with a value assigned by the server. To achieve what
    you want, changing the json tag for LeadNumber to read
    `json:"Lead_Number__c,omitempty"` should work just fine.

    -Cem
    On Wednesday, April 3, 2013 12:05:11 AM UTC-4, Jev Björsell wrote:

    Hello,

    I'm using json.Marshal to convert a struct to Json, for the purpose of
    posting said json to a rest end point.

    The struct looks like this;
    type Lead struct {
    LeadNumber string `json:"Lead_Number__c"`
    FirstName string
    LastName string
    Company string
    Email string
    Description string
    }

    After the post, I get the same resource back in the response.

    I need to exclude the LeadNumber filed from the POST, as that field is
    read only, and assigned by the server. I want to read that same value back
    into my struct from the http.Response

    How do I exclude a specific field from being converted when using
    json.Marshal? Keep in mind, I need to fill the field during json.Unmarshal.


    Thanks!
    -Jev
    --
    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.
  • Jev Björsell at Apr 3, 2013 at 3:09 pm
    ,omitempty is exactly what I was looking for, and works great. Thanks!
    On Tuesday, April 2, 2013 10:20:29 PM UTC-7, Cem Ezberci wrote:

    If my understanding is correct, when you marshal the struct, LeadNumber
    will be empty because you wouldn't be posting it with a value there since
    the server is supposed to assign it. However, response will come back with
    LeadNumber populated with a value assigned by the server. To achieve what
    you want, changing the json tag for LeadNumber to read
    `json:"Lead_Number__c,omitempty"` should work just fine.

    -Cem
    On Wednesday, April 3, 2013 12:05:11 AM UTC-4, Jev Björsell wrote:

    Hello,

    I'm using json.Marshal to convert a struct to Json, for the purpose of
    posting said json to a rest end point.

    The struct looks like this;
    type Lead struct {
    LeadNumber string `json:"Lead_Number__c"`
    FirstName string
    LastName string
    Company string
    Email string
    Description string
    }

    After the post, I get the same resource back in the response.

    I need to exclude the LeadNumber filed from the POST, as that field is
    read only, and assigned by the server. I want to read that same value back
    into my struct from the http.Response

    How do I exclude a specific field from being converted when using
    json.Marshal? Keep in mind, I need to fill the field during json.Unmarshal.


    Thanks!
    -Jev
    --
    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
postedApr 3, '13 at 4:12a
activeApr 3, '13 at 3:09p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase