Your input is a JSON object as text, presumably stored in a String
. You're essentially trying to convert that text to a JSON string. So do just that.
String input = "{\"name\":\"Error\",\"message\":\"hello\"}";
System.out.println("Original JSON content: " + input);
Gson gson = new Gson();
String jsonified = gson.toJson(input);
System.out.println("JSONified: " + jsonified);
Since quotes (and other characters) must be escaped in JSON strings, the toJson
will properly perform that transformation.
The code above will produce the
Original JSON content: {"name":"Error","message":"hello"}
JSONified: "{\"name\":\"Error\",\"message\":\"hello\"}"
In other words, jsonified
now contains the content of a JSON string.
With Jackson, the process is the same, just serialize the String
instance that contains your JSON object text
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, input);
jsonified = writer.toString();
System.out.println("JSONified: " + jsonified);
produces
JSONified: "{\"name\":\"Error\",\"message\":\"hello\"}"