🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.node.objectnode
com.fasterxml.jackson.databind.node.ObjectNode.remove java code examples | Tabnine
public static String toJson(Object o, String exclude) { final ObjectNode jsonNode = MAPPER.valueToTree(o); jsonNode.remove(exclude); return jsonNode.toString(); } ... @Override public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ObjectMapper mapper = (ObjectMapper) p.getCodec(); ObjectNode root = mapper.readTree(p); Iterator<Map.Entry<String, JsonNode>> iterator = root.fields(); while (iterator.hasNext()) { Map.Entry<String, JsonNode> entry = iterator.next(); if (entry.getKey().equals(TYPE_KEY)) { Class<? extends T> configClass = concreteFactory.apply(entry.getValue().asText()); root.remove(TYPE_KEY); return mapper.convertValue(root, configClass); } } throw new ConfigurationException("Failed to deserialize polymorphic " + _valueClass.getSimpleName() + " configuration"); } }
🌐
Program Creek
programcreek.com › java-api-examples
com.fasterxml.jackson.databind.node.ObjectNode#remove
private static void clearBaseFields(final ObjectNode node) { node.remove("attributes"); node.remove("Id"); node.remove("IsDeleted"); node.remove("CreatedDate"); node.remove("CreatedById"); node.remove("LastModifiedDate"); node.remove("LastModifiedById"); node.remove("SystemModstamp"); node.remove("LastActivityDate"); } ... /** * Recursively descend into the fields of the passed map and compare to the passed BQ schema, * while modifying the structure to accommodate map types, nested arrays, etc.
🌐
Baeldung
baeldung.com › home › json › jackson › removing json elements with jackson
Removing JSON Elements With Jackson | Baeldung
January 8, 2024 - We’d like to remove the element with the key age from the details nested object: @Test public void given_JsonData_whenUsingJackson_thenRemoveElementFromNestedStructure() throws JsonProcessingException { String json = "{\"name\": \"John\", \"details\": {\"age\": 30, \"city\": \"New York\"}}"; ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json); JsonNode detailsNode = jsonNode.path("details"); ((ObjectNode) detailsNode).remove("age"); String updatedJson = objectMapper.writeValueAsString(jsonNode); Assertions.assertEquals("{\"name\":\"John\",\"details\":{\"city\":\"New York\"}}", updatedJson); } In the above code, we access the nested object (details) and remove the element with the key (age).
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › node › ObjectNode.html
ObjectNode (Jackson JSON Processor)
Will return value of the field, if such field existed; null if not. public ObjectNode remove(Collection<String> fieldNames)
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › 6-5 › javadoc › com › fasterxml › jackson › databind › node › ObjectNode.html
ObjectNode (The Adobe AEM Quickstart and Web Application.)
Method for removing a property from this ObjectNode. Will return previous value of the property, if such property existed; null if not. ... Method for removing specified field properties out of this ObjectNode.
Top answer
1 of 2
5

I think that you don't want to change the object being passed to the method, So you have made a deep copy. And you want to return a new JSON object.

Am I right so far? If yes, As you have made the deep copy, use it, loop through each element of the array of your concern and replace the Node value with what you desire.

private static JsonNode attachmentConversion(JsonNode object){



    final String ATTACHMENT_POINTER = "/RESULTS/ATTACHMENTS/ATTACHMENT"; //Path to the array
    ObjectNode OUTPUT = object.deepCopy();
    OUTPUT.remove("DETAILS");

    //Validate is an array - It properly fetches the array
    if(OUTPUT.at(ATTACHMENT_POINTER).isArray()){
        int counter=0;

        //Loop through attachment array - It properly fethes each object in the array
        for(final JsonNode objNode : OUTPUT.at(ATTACHMENT_POINTER)){
            ((ObjectNode)objNode).replace("ATTACH_CONTENT", xmlToJson.parseXmlToJson(objNode.get("ATTACH_CONTENT"));
        }

    }
    return new ObjectMapper()
            .createObjectNode()
            .set("customertopology", OUTPUT);
}
2 of 2
2

Instead of providing entire path, you can do

OUTPUT.get("RESULTS").get("ATTACHMENTS").get("ATTACHMENT");

This should return the array. Full code using fasterxml

String json = "{\n" +
            "  \"RESULTS\": {\n" +
            "     \"ATTACHMENTS\": {\n" +
            "       \"ATTACHMENT\": [{\"key\": 1}, {\"key\": 2}]\n" +
            "  }\n" +
            " }\n" +
            "}";

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
node.get("RESULTS").get("ATTACHMENTS").get("ATTACHMENT").forEach(obj -> {
        System.out.println(obj.get("key"));
    });

Also following code print true for me.

System.out.println(node.at("/RESULTS/ATTACHMENTS/ATTACHMENT").isArray());
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonnode.html
Jackson JsonNode
Here is an example that removes a field from a Jackson ObjectNode via its remove() method:
Find elsewhere
🌐
Baeldung
baeldung.com › home › json › jackson › working with tree model nodes in jackson
Working with Tree Model Nodes in Jackson | Baeldung
January 8, 2024 - JsonNode removedNode = ... which returns the parent node instead of the one to be removed: ObjectNode locatedNode = locatedNode.remove(fieldNames);...
🌐
Tabnine
tabnine.com › home page › code › java › org.codehaus.jackson.node.objectnode
org.codehaus.jackson.node.ObjectNode.remove java code examples | Tabnine
@Override public void removeField(String fieldName) { getNode().remove(fieldName); for (FieldProperty fp : FieldProperty.values()) { removeFieldProperty(fieldName, fp.getName()); } } ... @Override public void removeProperty(String name) { JsonNode child = node.get(name); if (child!=null && !child.isArray() && !child.isObject()) getNode().remove(name); } ... private void convertTetheredCable(final JsonNode obj) { final Iterator<JsonNode> nodes = obj.path("Connector").iterator(); while (nodes.hasNext()) { final ObjectNode node = (ObjectNode) nodes.next(); final JsonNode current = node.path("TetheredCable"); if (!current.isMissingNode() && !current.isNull()) { final int value = Integer.parseInt(current.asText()); node.put("TetheredCable", value); } if (current.isNull()) { node.remove("TetheredCable"); } } }
🌐
Red Hat
access.redhat.com › webassets › avalon › d › red-hat-jboss-enterprise-application-platform › 7.0.0 › javadocs › com › fasterxml › jackson › databind › node › ObjectNode.html
ObjectNode (Red Hat JBoss Enterprise Application Platform 7.0.0.GA public API)
Deprecated. Since 2.4 use setAll(ObjectNode), Method for adding all properties of the given Object, overriding any existing values for those properties. ... Method for removing all field properties out of this ObjectNode except for ones specified in argument.
🌐
Javadoc.io
javadoc.io › static › com.fasterxml.jackson.core › jackson-databind › 2.14.2 › com › fasterxml › jackson › databind › node › ObjectNode.html
ObjectNode (jackson-databind 2.14.2 API)
Method for removing a property from this ObjectNode. Will return previous value of the property, if such property existed; null if not. ... Method for removing specified field properties out of this ObjectNode.
🌐
Javadoc.io
javadoc.io › static › com.fasterxml.jackson.core › jackson-databind › 2.13.3 › com › fasterxml › jackson › databind › node › ObjectNode.html
ObjectNode (jackson-databind 2.13.3 API)
Method for removing a property from this ObjectNode. Will return previous value of the property, if such property existed; null if not. ... Method for removing specified field properties out of this ObjectNode.
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 12116519 › using-jsonnode-or-objectnode-how-can-i-remove-an-item-from-a
Using JsonNode or ObjectNode how can i remove an item from a kotlinlang #getting-started
Using JsonNode or ObjectNode how can i remove an item from a nested list if found Lets say I have this JSON ``` A Test someNode X Test myList RemoveThis DontRemove ``` How can I remove `RemoveThis` I