title: Jackson Json Map or Key deserializer array data to Java hashmap<Integer,MyClass>
body: <p>New to Jackson and json so I appologize if I'm not clear enough.</p>
  
  <p>I've seen some references to Key and Map Deserializers that might be a better fit for what I want to do but I haven't been able to locate a concrete example on how these would be put together. </p>
  
  <p>I am looking for the best way to deserialize a JSON Array into a Hashmap where the hashmap key is the one of the values is the passed JSON Array. This could either be a change in the JSON or a change in the serialization.</p>
  
  <p>This is my current code design prototype. eventually this will be part of a camel route.</p>
  
  <p>Parent class</p>
  
  <pre><code>@XmlRootElement(name = "EligibilityUnit")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class EligibilityUnit implements Serializable {
  private static final long serialVersionUID = 3456126687901598200L;
  @XmlElement(name = "EligibilityUnitUUID")
  protected int eligibilityUnitUUID;
  
  @JsonProperty("eligibilityMembers")
  @JsonDeserialize(using = Elig4Deserializer.class) // JSONtoMap2
  protected   HashMap&lt;Integer, EligibilityMember&gt; eligibilityMembers;
  public EligibilityUnit() {
      super();
      eligibilityUnitUUID =  Math.abs(UUID.randomUUID().hashCode());
      }
  
  public Map &lt;Integer,EligibilityMember&gt; getEligibilityMembers() {
      return eligibilityMembers;
  }
  public void setEligibilityMembers(HashMap&lt;Integer, EligibilityMember&gt; eligibilityMembers) {
      this.eligibilityMembers = eligibilityMembers;
  }
  ... getters/setters
  }
  </code></pre>
  
  <p>Child class</p>
  
  <pre><code>@XmlRootElement(name = "EligibilityMember")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class EligibilityMember implements Serializable {
  private static final long serialVersionUID = -5943362268820173933L;
  Integer personId;  
  String firstName;
  String lastName;
  //... getters/setters
  }
  </code></pre>
  
  <p>Test program</p>
  
  <pre><code>public static void main(String[] args)
  { 
      String json =               
              "{"+
                        "\"eligibilityMembers\": " +
                      "["+
                              "{"+
                              "\"personId\": 11,"+
                              "\"firstName\": \"Jane\","+
                              "\"lastName\": \"Jones\""+
                            "},"+
                            "{"+
                              "\"personId\": 12,"+
                              "\"firstName\": \"Tiffany\","+
                              "\"lastName\": \"Roberts\""+
                            "},"+
                            "{"+
                              "\"personId\": 13,"+
                              "\"firstName\": \"Janika\","+
                              "\"lastName\": \"Brown\""+
                            "}"+
                        "]"+
                      "}";
      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);  
      Object map = mapper.readValue(json, EligibilityUnit.class);
      //Convert Map to JSON
       String jsonOut = mapper.writeValueAsString(map);
       //Print JSON output
       System.out.println(jsonOut);
  
  }
  </code></pre>
  
  <p>De-serialization Class</p>
  
  <pre><code> public class Elig4Deserializer extends JsonDeserializer {
   public Elig4Deserializer() {
       super();
  
      }
  
  
  @Override
  public Object deserialize(JsonParser arg0, DeserializationContext arg1)
          throws IOException, JsonProcessingException {
      ObjectMapper mapper = new ObjectMapper();
      JsonNode node = arg0.readValueAsTree();
      String json =  node.toString();
      Object map = null;
      Map&lt;Integer,EligibilityMember&gt; mapx = new HashMap&lt;Integer,EligibilityMember&gt;();
  
      CollectionType javaType = mapper.getTypeFactory()
                .constructCollectionType(List.class, EligibilityMember.class);
  
      List&lt;EligibilityMember&gt; members =    mapper.readValue(json, javaType);
  
       for (EligibilityMember i : members) mapx.put(i.getPersonId(),i);
      return (Object) mapx;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the json from the class</p>
  
  <pre><code>{"eligibilityUnit":1062082797,"eligibilityMembers":{"11":   {"personId":11,"personSSN":null,"personNumber":null,"primeNumber":null,"individualId":null,"firstName":"Jane","lastName":"Jones"},"12":{"personId":12,"personSSN":null,"personNumber":null,"primeNumber":null,"individualId":null,"firstName":"Tiffany","lastName":"Roberts"},"13":{"personId":13,"personSSN":null,"personNumber":null,"primeNumber":null,"individualId":null,"firstName":"Janika","lastName":"Brown"}}}
  </code></pre>
  
  <p>Is this the best way to handle this? I've seen a lot of partial solutions but nothing that quite matches what I am trying to do. </p>
  
  <p>Realistically, I would prefer the JSON was something like this <em>(I understand the format on this my not be 100% correct I am posting it as where I would like to go. it is still a work in progress)</em></p>
  
  <p><code>{"eligibilityMembers":
      [{
       "personId":11,
             {"firstName":"Jane","lastName":"Jones",
                   "Income"
                         [{"IE-ME":
                              [{"Wages":1434.34,"support":535.99}],
                          "FS":
                              [{"Misc":60.00,"SSI":754.55}],
                          "CM":
                              [{"Support":535.99}],
                          }]
                  "Expenses":
                         [{"IE-ME":
                              [{"Rent":908.90,"Utilities":45.99}],
                          "DCS":
                              [{"Misc":60.00,"ChildCare":754.55}]
                          }]
              },
       {"personId":12,
            {"firstName":"Tiffany","lastName":"Roberts"
                         "Income"
                         [{"IE-ME":
                              [{"Wages":1.34}],
                          "FS":
                              [{"SSD":50.55}],
                          "CSC":
                              [{"Support":535.99}],
                          }]
                  "Expenses":
                         [{"IE-ME":
                              [{"Rent":400.15,"Utilities":80.86}] 
                          }]
            },
       {"personId":13, 
            {"firstName":"Janika","lastName":"Brown"}
       }]
  }
  }</code></p>