title: Convert json String containing array to Map in Java
body: <p>I need to convert String to Map for the following json string in Java:
  Please note that this json string has array in it and that is where I am facing the issue:</p>
  
  <pre><code>{
     "type":"auth",
     "amount":"16846",
     "level3":{
        "amount":"0.00",
        "zip":"37209",
        "items":[
           {
              "description":"temp1",
              "commodity_code":"1",
              "product_code":"11"
           },
           {
              "description":"temp2",
              "commodity_code":"2",
              "product_code":"22"
           }
        ]
     }
  }
  </code></pre>
  
  <p>I tried couple of ways as mentioned in below links:</p>
  
  <p><a href="https://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/" rel="nofollow noreferrer">Convert JSON string to Map – Jackson</a></p>
  
  <p><a href="https://stackoverflow.com/a/22011887/948268">Parse the JSONObject and create HashMap</a></p>
  
  <p>Error I am getting:</p>
  
  <blockquote>
    <p>JSON parser error: Can not deserialize instance of java.lang.String
    out of START_OBJECT token ... }; line: 3, column: 20] (through
    reference chain:
    java.util.LinkedHashMap["level3"])com.fasterxml.jackson.databind.JsonMappingException:
    Can not deserialize instance of java.lang.String out of START_OBJECT
    token</p>
  </blockquote>
  
  <p>So to give more details about what I am doing with the Map is, this map will be converted back to the json string using following method:</p>
  
  <pre><code>    public static String getJSON(Object map) throws IOException {
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      OutputStream stream = new BufferedOutputStream(byteStream);
      JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(stream, JsonEncoding.UTF8);
      objectMapper.writeValue(jsonGenerator, map);
      stream.flush();
      jsonGenerator.close();
      return new String(byteStream.toByteArray());
  }
  </code></pre>