title: Jackson JSON Serialization without field name
body: <p>I have a JAVA POJO which has many fields. One of the fields is <code>Map&lt;String, Object&gt;</code> for which I am using the <code>Custom JsonSerializer</code> as it can have many type of <code>Objects</code>. All I want to know is how can I avoid the Serialization of the <code>fieldname</code> only for this <code>Map&lt;String,Object&gt;</code> field. For all other fields in POJO, I would like to have the field name but only for this, I want to remove it.</p>
  <p>As of now when use <code>Jackson</code> searlizer then I get the following output:</p>
  <pre><code>{
    &quot;isA&quot; : &quot;Human&quot;,
    &quot;name&quot; : &quot;Batman&quot;,
    &quot;age&quot; : &quot;2008&quot;,
    &quot;others&quot; : {
      &quot;key1&quot; : &quot;value1&quot;,
      &quot;key2&quot; : {
        &quot;key3&quot; : &quot;value3&quot;
      },
      &quot;key5&quot; : {
        &quot;key4&quot; : &quot;One&quot;,
        &quot;key4&quot; : &quot;Two&quot;
      }
    }
  }
  </code></pre>
  <p>I want to get the following output: (All I want to do is remove the <code>Map&lt;String,Object&gt;</code> field name but keep its children.)</p>
  <pre><code>{
    &quot;isA&quot; : &quot;Human&quot;,
    &quot;name&quot; : &quot;Batman&quot;,
    &quot;age&quot; : &quot;2008&quot;,
    &quot;key1&quot; : &quot;value1&quot;,
    &quot;key2&quot; : {
      &quot;key3&quot; : &quot;value3&quot;
    },
    &quot;key5&quot; : {
      &quot;key4&quot; : &quot;One&quot;,
      &quot;key4&quot; : &quot;Two&quot;
    }
  }
  </code></pre>
  <p>Following is my <code>Human.class</code> POJO which is used by <code>ObjectMapper</code>:</p>
  <pre><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = &quot;isA&quot;)
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @JsonIgnoreProperties(ignoreUnknown = true)
  @Getter
  @Setter
  @NoArgsConstructor
  @ToString
  class Human {
      private String isA;
      private String name;
      private String age;
  
      @JsonSerialize(using = MyCustomSearlize.class)
      private Map&lt;String, Object&gt; others = new HashMap&lt;&gt;();
  }
  </code></pre>
  <p>Following is my <code>Custom searlizer</code> which is used by MAP during searlization:</p>
  <pre><code>class MyCustomSearlize extends JsonSerializer&lt;Map&lt;String, Object&gt;&gt; {
  
      private static final ObjectMapper mapper = new ObjectMapper();
  
      @Override
      public void serialize(Map&lt;String, Object&gt; value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
          gen.writeStartObject();
          recusiveSerializer(value, gen, serializers);
          gen.writeEndObject();
      }
  
      public void recusiveSerializer(Map&lt;String, Object&gt; value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
          for (Map.Entry&lt;String, Object&gt; extension : value.entrySet()) {
              if (extension.getValue() instanceof Map) {
                  //If instance is MAP then call the recursive method
                  gen.writeFieldName(extension.getKey());
                  gen.writeStartObject();
                  recusiveSerializer((Map) extension.getValue(), gen, serializers);
                  gen.writeEndObject();
              } else if (extension.getValue() instanceof String) {
                  //If instance is String directly add it to the JSON
                  gen.writeStringField(extension.getKey(), (String) extension.getValue());
              } else if (extension.getValue() instanceof ArrayList) {
                  //If instance if ArrayList then loop over it and add it to the JSON after calling recursive method
                  for (Object dupItems : (ArrayList&lt;Object&gt;) extension.getValue()) {
                      if (dupItems instanceof Map) {
                          gen.writeFieldName(extension.getKey());
                          gen.writeStartObject();
                          recusiveSerializer((Map) dupItems, gen, serializers);
                          gen.writeEndObject();
                      } else {
                          gen.writeStringField(extension.getKey(), (String) dupItems);
                      }
                  }
              }
          }
      }
  }
  
  
  </code></pre>
  <p>Following is my <code>Main</code> class:</p>
  <pre><code>public class Main {
      public static void main(String[] args) throws JsonProcessingException {
  
          Human person = new Human();
  
          person.setName(&quot;Batman&quot;);
          person.setAge(&quot;2008&quot;);
          Map&lt;String, Object&gt; others = new HashMap&lt;&gt;();
          others.put(&quot;key1&quot;, &quot;value1&quot;);
          Map&lt;String, Object&gt; complex = new HashMap&lt;&gt;();
          complex.put(&quot;key3&quot;, &quot;value3&quot;);
          others.put(&quot;key2&quot;, complex);
          Map&lt;String, Object&gt; complex2 = new HashMap&lt;&gt;();
          List&lt;String&gt; dup = new ArrayList&lt;&gt;();
          dup.add(&quot;One&quot;);
          dup.add(&quot;Two&quot;);
          complex2.put(&quot;key4&quot;, dup);
          others.put(&quot;key5&quot;, complex2);
          person.setOthers(others);
  
          final ObjectMapper objectMapper = new ObjectMapper();
          SimpleModule simpleModule = new SimpleModule();
          objectMapper.registerModule(simpleModule);
          final String jsonEvent = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
          System.out.println(jsonEvent);
      }
  }
  </code></pre>
  <p>Following things I tried:</p>
  <ol>
  <li>I tried to add <code>@JsonValue</code> on the <code>Map</code> but this will remove all my other values (name,age, isA, etc.)</li>
  <li>I tried the <code>@JsonAnyGetter</code> this works for <code>Map</code> and <code>String</code> but does not work for <code>ArrayList</code> as I want. I am handling the <code>ArrayList</code> bit differently in my application as a part of my requirement.</li>
  </ol>
  <p>Is there a way to may it work with <code>@JsonSerialize</code> and <code>@JsonAnyGetter</code> because I am unable to use both together.</p>
  <p>Can someone please help in solving this issue? Please guide me to appropriate documentation or workaround thanks a lot.</p>