title: Java dynamic Map<String, Map> merging to be converted to a JSON
body: <p>I'm using a <code>Map&lt;String, Map&gt; resultMap</code> having values like this:</p>
  <pre><code>[
      {main={group={street={cnt=Burundi, zip=123}, code=asd, subtype={OrgType=1,2}}}},
      
      {main={info={text=bbb}}},
      
      {main={group={subId=5, attrib={attribId=1,2}}}}
  ]
  </code></pre>
  <p>I have to create a JSON out of those Maps that should look like this:</p>
  <pre><code>        { &quot;main&quot;: [ 
              { 
                 &quot;group&quot;: { 
                      &quot;street&quot;: {
                           &quot;cnt&quot;: &quot;Burundi&quot;,
                           &quot;zip&quot;: &quot;123&quot;
                      },
                      &quot;code&quot;: &quot;asd&quot;,
                      &quot;subtype&quot;: {
                           &quot;OrgType&quot;: &quot;1,2&quot;
                      },
                      &quot;subId&quot;: &quot;5&quot;,
                      &quot;attribId&quot;: &quot;1,2&quot;
                  },
                  &quot;info&quot;: {
                      &quot;text&quot;: &quot;bbb&quot;
                  }
               }
           ]
        }
  </code></pre>
  <p>Any idea how could I merge those Maps doing a <code>GSON.toJson(resultMap);</code> to obtain this JSON ?</p>
  <p>I tried to merge the Map</p>
  <pre><code>    public String produceJsonFromMap(Map&lt;String, Map&gt; initialMap) {
  
           Map&lt;String, Object&gt; resultMap = new HashMap&lt;&gt;();
  
           for(Map map: initialMap.values()) {
                resultMap.putAll(map);
           }
           return GSON.toJson(resultMap);
        }
  </code></pre>
  <p>But this overwrites my previous maps and I don't know how can I obtain the desired merged map...</p>