Skip to content
Advertisement

How to avoid Jackson escaping double quotes in Java?

I am working on a project and wanted to rewrite some code written in Gson to Jackson using ObjectMapper. So I am trying to create a JSON string using some properties as below:

JavaScript

Expected result:

JavaScript

Actual:

JavaScript

So once I create a JSON String using Jackson, it turns out it is escaping double quotes for field names and values and adding a at leading and trailing spaces. So service call fails due to the escaping.

I have went through some documentation and I can understand Jackson is escaping double quotes. But is there a way to avoid escaping double quotes and adding of leading and trailing double quotes.

Any help is appreciated. BTW, I followed the links below:

Why ObjectNode adds backslash in in Json String

Jackson adds backslash in json

Advertisement

Answer

The problem is you are converting your JSON object to a String via its toString() method before passing it to the objectMapper. Change this:

JavaScript

to this:

JavaScript

You also need to change this:

JavaScript

to this:

JavaScript

and it will work as you expect.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement