Search code examples
javajacksonjackson2

Merge JSON String with Map using Jackson without deserializing the String


Let say I have a String {"foo": "bar"} and a Map Map("hello" -> "world").

Using Jackson, how do I combine them into a String {"foo": "bar", "hello": "world"} without deserializing the foo-bar String first?

The foo-bar String comes from another system (trusted, will return valid JSON String), and it's inefficient to deserialize it into Map just to add a few properties, and then serializing it back to String.


Solution

  • Replace the end brace with a comma.
    Produce the JSON string for your map.
    Remove the start brace from your map JSON.
    Concatinate the strings.