I have a json like below. The x’s are there to omit sensitive data
{"errormessage":"{"timestamp":"2021-10- 19T07:57:35.205+0000","status":400,"error": "Bad Request","message":"Bad Request: xxxx xxx xxx xxx xxx. pathu003d/xxx/verify","path":"/xxx /xxx"}"}
I am trying to deserialize this json into the class below
public class TransferResponse {
private Optional<String> timeStamp = Optional.empty();
private Optional<String> status = Optional.empty();
private Optional<String> error = Optional.empty();
@JsonAlias(value = {"errorMessage", "errormessage"})
private Optional<String> message = Optional.empty();
private Optional<String> path = Optional.empty();
}
They way I am trying to deserialize it is given below
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
final String nestedErrorResponse = "{"errormessage":"{"timestamp":"2021-10- 19T07:57:35.205+0000","status":400,"error":"Bad Request","message":"Bad Request: xxxx xx is xx xxx. pathu003d/xxx/xxx","path":"/xxxx /xxx"}"}";
TransferResponse responseObject = objectMapper.readValue(nestedErrorResponse, TransferResponse.class);
But I am getting an exception like below
Unexpected character ('t' (code 116)): was expecting comma to separate Object entries
at [Source: (String)"{"errormessage":"{"timestamp":"2021-10- 19T07:57:35.205+0000","status":400,"error":"Bad Request","message":"Bad Request: xxxx xx is xx xxx. path=/xxxxx/verify","path":"/xxxxx /xxxxx"}"}"; line: 1, column: 21]
I want to be able to read the value against errormessage as string. Could anyone help me with this?
2
Answers
You need to escape the slashes when you provide a json string with nested json programatically – add a slash and escape it at once, that makes nested quotes have 3 slashes:
I use Intellij Idea and whrn I copy a json string into another string, it adds the proper number of slashes automatically.
Can you convert your JSON data as shown below and test your code, hopefully, this will help you to fix the issue. I usually test my JSON data in online Json data validator t. ex https://jsonformatter.curiousconcept.com/#