title: Is there a way to marshal different struct type to same json field when not empty?
body: <p>I have a struct for response defined in common package like this.</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;encoding/json&quot;
      &quot;fmt&quot;
  )
  
  type Response struct {
      Id string `json:&quot;id,omitempty&quot;`
      //All other int/string fields
  
      NestedObj *Nested `json:&quot;nested,omitempty&quot;`
  }
  
  type Nested struct {
      //Field causing the issue.
      ExtendedString string `json:&quot;extended,omitempty&quot;` //some user will need/expect a string
  
      //Other user need extended json field as a different object
      Extended *Extended `json:&quot;extended,omitempty&quot;` //changing json:extended to other json:extended1 will work as expected
                                                      //But need to have the same field name containing object or just a string field
  }
  
  type Extended struct {
      OtherField string `json:&quot;another,omitempty&quot;`
  }
  
  func main() {
      response := &amp;Response{Id: &quot;UNIQUE_ID&quot;}
  
      response.NestedObj = &amp;Nested{}
  
      //Part 1
      response.NestedObj.ExtendedString = &quot;value&quot;
  
      b, err := json.Marshal(response)
      if err != nil {
          fmt.Println(err)
      }
      fmt.Println(string(b))
      //Part 1 end
  
      
      response1 := &amp;Response{Id: &quot;UNIQUE_ID&quot;}
  
      response1.NestedObj = &amp;Nested{}
  
      //Part 2
      response1.NestedObj.Extended = &amp;Extended{}
  
      response1.NestedObj.Extended.OtherField = &quot;value&quot;
  
      b1, err := json.Marshal(response1)
      if err != nil {
          fmt.Println(err)
      }
      fmt.Println(string(b1))
      //Part 2 end
      
      //Either Part 1 or 2 will be set for user type as per preference/flag/condition. 
  
  }
  
  
  </code></pre>
  <p>Now as I have use pointer in struct to not export any empty field/object in json, I want to behave it like
  when I set <code>ExtendedString</code> to some string value and omit the <code>Extended</code> custom struct my json should look like</p>
  <pre><code>{&quot;id&quot;:&quot;UNIQUE_ID&quot;,...other json fields..., &quot;nested&quot;:{&quot;extended&quot;: &quot;value&quot;}}
  </code></pre>
  <p>But when I set <code>Extended </code> struct ignoring <code>ExtendedString</code> I should get the json as</p>
  <pre><code>{&quot;id&quot;:&quot;UNIQUE_ID&quot;,...other json fields..., &quot;nested&quot;:{&quot;extended&quot;: {&quot;another&quot;:&quot;value&quot;}}}
  </code></pre>
  <p>It is sure that either one of them will be needed in marshaled json based on user preference(<code>if</code> or <code>switch</code> condition may be) as they have to have the field name as <code>extended</code> which can either be a string or json object containg other fields.</p>
  <p>Is it possible to achieve this without duplicating the main struct?</p>