Search code examples
javajsonjacksonjackson-databind

Serialized JSON String as Value in JSON


I want a serialized JSON string to be treated simply as a string in my JSON when reading it using Jackson. When I simply escape the serialized JSON string and use it as a value, the serialized string gets treated as part of the JSON and parsed. Any ideas as to how to go about doing this?

For example:

 "{\"payload\":\"{id:\"some-random-id\",version:554471325}\"}"

I would like this to be read in memory something like the following:

{ payload: "{id:\"some-random-id\",version:554471325}" }

However, the parser is trying to read the serialized string as JSON and turn it into the following:

{ payload: {id:"some-random-id", version:554471325} }

Note the difference between the two outputs. In one case, the value associated with payload is a string, in the other it's a JSON object. I'm trying to get the former, what I'm getting instead is an attempt at the latter.


Solution

  • I know this is a terribly worded question, and I apologize for that. However, I can't come up with a better way of wording it and I'm unable to supply code. For anyone who stumbles on this and understands what I'm asking, I found a workaround.

    I encoded the serialized JSON string to base64 so it was detected by Jackson as a string rather than a JSON object.

    The example in the question above becomes:

     "{\"payload\":\"e2lkOiJhbnktZXhlY3V...\"}"
    

    where e2lkOiJhbnktZXhlY3V... is a base64 encoded representation of the string "{id:\"some-random-id\",version:554471325}"