2

Java String which I retrieved from the database:

[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo" }
]

How can I convert it to a JsonPatch object?

I am using com.github.fge.jsonpatch library.

Ayush
  • 206
  • 7
  • 22

1 Answers1

3

Based on JSON Patch documentation, you can build a JsonPatch instance using Jackson deserialization.

 String json = "...";

 final ObjectMapper mapper = new ObjectMapper();
 final InputStream in = new ByteArrayInputStream(json.getBytes());
 final JsonPatch patch = mapper.readValue(in, JsonPatch.class);
obourgain
  • 8,856
  • 6
  • 42
  • 57
  • Works like magic! I had error when just using "objectMapper.convertValue(inputJsonString, JsonNode.class)", the escaped double quotes would stay and it would be invalid json... – solujic Feb 23 '23 at 13:12