1

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:

{
   "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"
         }
      ]
   }
}

I tried couple of ways as mentioned in below links:

Convert JSON string to Map – Jackson

Parse the JSONObject and create HashMap

Error I am getting:

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

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:

    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());
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Learner
  • 1,503
  • 6
  • 23
  • 44
  • 1
    Facing what issue? What have you tried? How did it not work? If you [edit] your question to include your attempt(s), it'd be a lot easier to figure out where you're getting stuck. – azurefrog Jan 27 '19 at 23:03
  • Added details, essentially I want to know that does the way I am trying to do it will also take care of Array within the json string? – Learner Jan 27 '19 at 23:07
  • map appropriates for key-value pairs from json, but you have complex data structure. Which elements do you have to extract from json? – Filomat Jan 27 '19 at 23:10
  • 1
    It would also be useful to know what you expected the map to look like. For example, what should happen with the 'level3' object. – Karl Galvez Jan 27 '19 at 23:11
  • Added details about what I need to do with the HashMap. – Learner Jan 27 '19 at 23:18
  • 2
    You shouldn't be using a Map for this. Create a proper class structure and use a library like Jackson to deserialize your JSON. – shmosel Jan 27 '19 at 23:19
  • It is actually the existing code in the project but now I have scenario where we could also get array within it. So, cannot change it, I need to have the Map. – Learner Jan 27 '19 at 23:53

1 Answers1

3

You cannot parse your JSON content into a Map<String, String> (like it is done in the two links you posted). But you can parse it into a Map<String, Object>.

For example like this:

ObjectMapper mapper = new ObjectMapper();
File file = new File("example.json");
Map<String, Object> map;
map = mapper.readValue(file, new TypeReference<Map<String, Object>>(){});
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49