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;

public void removeNode() {

    JsonObject project = createObjectBuilder().
            add("project", "thinwars").
            add("dev", createObjectBuilder().
                    add("name", "duke").
                    add("age", 18)).
    JsonPatch patch = Json.createPatchBuilder().
    JsonObject projectWithoutDeveloper = patch.apply(project);


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.

Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.


Personally, I found JsonPatch difficult to use and to extend. For that I made a simple Object-oriented JSON
What do you think about it?

Posted by piotrkot on February 20, 2019 at 10:43 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments