Am facing issue while parsing JSON string with Jackson in some cases.
String jsonString = "{\"Age\":40, \"Name\":\"Sample User\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString,JsonNode.class);
System.out.println(jsonstirng)
{"Age":40, "Name":"Sample User"}
Above code works well when I pass jsonString value.
In some cases I need to escape invalid string characters like ",' etc
For escaping I am using Apache StringEscapeUtils.
String escapedString = StringEscapeUtils.escapeJson(jsonStirng);
Escaped String output
{\"Age\":40,\"Name\":\"Sample User\"}
When I pass escapedString to mapper its throw Unexpected character exception.
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(escapedString,JsonNode.class);
Exception
Unexpected character ('\' (code 92)): was expecting double-quote to start field name
Actually Am parsing ModSecurity audit logs. Response body of audit log has (html,css,javascript etc) stuff that's why I need to escape JSON string other wise its breaks the JSON format.