1

I need to modify JSONPatch before applying it to the main object, I search a lot but didn't find any solution.

I have a JSONPatch request something like this:

[op: replace; path: "/size"; value: "1", op: replace; path: "/name"; value: "test"]

Now in the below code, I want a loop for this JSONPatch object to modify some values (for example name).

public void patch(JsonPatch jsonPatch) throws JsonPatchException {
   
   // need a foreach here to access JSONPatch object to modify some values

   jsonPatch.apply(objectMapper.convertValue(myObject, JsonNode.class));
   
}
  • I understand you meant `[{op: replace; path: "/size"; value: "1"}, {op: replace; path: "/name"; value: "test"}]`. Have you tried simply binding the patch to a `List patches` instead? – crizzis Mar 31 '21 at 17:16
  • unfortunately, it can't, because the type of the object is JsonPatch :( @crizzis – Mohammadreza Yektamaram Mar 31 '21 at 17:52

1 Answers1

0

Found a solution by doing something like this:

var jsonPatchList = objectMapper.convertValue(jsonPatch, JsonNode.class);

for(int i = 0; i < jsonPatchList.size(); i++) {
    log.debug("Path: {}", jsonPatchList.get(i).get("path"));
    log.debug("Value: {}", jsonPatchList.get(i).get("value"));
}