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.


NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Personally, I found JsonPatch difficult to use and to extend. For that I made a simple Object-oriented JSON
https://github.com/piotrkot/oojson
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
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License