-1

I'm calling a JSON REST service and in the returned response, I have escape characters.

I want to avoid the escape characters (to have them interpreted), and pretty print a JSON response. I cannot use GSON because of some constraints. I've tried the pretty print option, but that doesn't seem to work.

All of this has to be done in JAVA.

For eg.

String originalString = "{\n    "name":"xyz",\n    "age":"23"\n}"

is available as a String. This has to be converted to

String newString = {"name":"xyz","age":"23"}

If possible, I should be able to pretty print it in the logs with proper indentation - something like,

{
    "name":"xyz",
    "age":"23"
}

I'm getting the original string in the logs from a REST JSON service response. The new string can be printed somehow in the logs (StringBuilder or some other way).

EDIT: Going up one level, the reason this question was asked is to print the request/responses in the logs. I've used this as an input while doing it for the request. However, after logging the request, I'm facing an issue logging the response. The problem reads,

Request processing failed; nested exception is java.lang.IllegalStateException: SRVE0209E: Writer already obtained

Has anyone faced a similar issue? The problem I believe is being able to cache the response like it is being done for the request in the example link above.

Community
  • 1
  • 1
Ramana Sarva
  • 293
  • 2
  • 5
  • 20

1 Answers1

1

Using Apache commons-lang StringEscapeUtils class you can achieve this. Below is the snippet for the same.

StringEscapeUtils.unescapeHtml(originalString);