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());
}
7
  • 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. Commented Jan 27, 2019 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? Commented Jan 27, 2019 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? Commented Jan 27, 2019 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. Commented Jan 27, 2019 at 23:11
  • 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. Commented Jan 27, 2019 at 23:19

1 Answer 1

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>>(){});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.