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.


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.

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