Deserialize JSON into existing object (Java)

Jonas N picture Jonas N · Sep 20, 2012 · Viewed 20.1k times · Source

I'd like to know how one might get the Jackson JSON library to deserialize JSON into an existing object? I've tried to find how to to this; but it seems to only be able to take a Class and instantiate it itself.

Or if not possible, I'd like to know if any Java JSON deserialization libraries can do it.

This seems to be a corresponding question for C#: Overlay data from JSON string to existing object instance. It seems JSON.NET has a PopulateObject(string,object).

Answer

Johan Boberg picture Johan Boberg · Nov 22, 2013

You can do this using Jackson:

mapper.readerForUpdating(object).readValue(json);

See also Merging Two JSON Documents Using Jackson