JSON-P: Removing a slot from a JsonObject with JsonPatch 📎
With JsonPatch
, you can remove a node, or perform multiple operations on an JsonObject
instance:
import javax.json.Json;
import static javax.json.Json.createObjectBuilder;
import javax.json.JsonObject;
import javax.json.JsonPatch;
@Test
public void removeNode() {
JsonObject project = createObjectBuilder().
add("project", "thinwars").
add("dev", createObjectBuilder().
add("name", "duke").
add("age", 18)).
build();
JsonPatch patch = Json.createPatchBuilder().
remove("/dev").
build();
JsonObject projectWithoutDeveloper = patch.apply(project);
assertFalse(projectWithoutDeveloper.containsKey("dev"));
System.out.println(projectWithoutDeveloper);
}
The output is: {"project":"thinwars"}