adam bien's blog

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"}

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.