Skip to content
Advertisement

Convert Java object to json string containing a json string property already

Consider a Java object like following:

JavaScript

so when we do,

JavaScript

I was expecting something like:

JavaScript

instead I got it as a json value wrapped into a double quotes and skipped by backslashes as it was double jsonized – if this is actually a verb –

JavaScript

Advertisement

Answer

I managed to overcome this problem by using @JsonRawValue

From documentation https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonRawValue.html it states the following:

Marker annotation that indicates that the annotated method or field should be serialized by including literal String value of the property as is, without quoting of characters. This can be useful for injecting values already serialized in JSON or passing javascript function definitions from server to a javascript client. Warning: the resulting JSON stream may be invalid depending on your input value.

So by doing,

JavaScript

I kinda told Jackson that this is a json value already don’t do your job here so same code in post generated the correct json

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