ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.createObjectNode();

or you can also do like you said in the comment above,

JsonNode node = JsonNodeFactory.instance.objectNode();

after that you can map the values,

JsonNode node = mapper.valueToTree(fromValue);
Answer from sanurah on Stack Overflow
🌐
Baeldung
baeldung.com › home › json › jackson › working with tree model nodes in jackson
Working with Tree Model Nodes in Jackson | Baeldung
January 8, 2024 - This is the most common way to create a node out of nothing: ... This method is well covered in the Jackson – Marshall String to JsonNode article.
Discussions

json - how to create insert new nodes in JsonNode? - Stack Overflow
But looking at the APIs for Jackson's JsonNode (v1.8) does not show any method as such. More on stackoverflow.com
🌐 stackoverflow.com
Generate an empty set of Json braces- using java & JSON - Stack Overflow
How to generate an empty json node using jackson-java. I tried with NullNode.instance, but that returns ... Whereas I want totals to be an empty instance. { "totals": {}, "orderId": "550047004", "numberOfItems": 2 } ... You should use ObjectNode. It can be created with ObjectMapper: ... Thanks! Just now I tried , new ObjectNode(JsonNodeFactory... More on stackoverflow.com
🌐 stackoverflow.com
July 21, 2018
How to create a empty JSONNode? - Unity Engine - Unity Discussions
If i give it a parse empty bracket string like this it works. Gotta be another way · I’m really looking for a way to do it with in the JSONNode class. I just checked JSONUtility documentation I don’t see anything that covers this. Seems id have to do it the same way as how im doing now ... More on forum.unity.com
🌐 forum.unity.com
0
August 18, 2019
Add `JsonNode.isEmpty()` as convenience alias
There was an error while loading. Please reload this page · An often-used idiom for checking for empty Arrays and Object for JsonNode is: More on github.com
🌐 github.com
0
December 11, 2018
🌐
YouTube
youtube.com › watch
How to Create an Empty JsonNode in Java Using Jackson - YouTube
Learn how to create an empty JsonNode in Java using Jackson's ObjectMapper and JsonNodeFactory. Get step-by-step guidance and code examples!---This video is ...
Published   March 30, 2025
Views   2
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonnode.html
Jackson JsonNode
Let me just spend a moment to explore how you handle null values in the Jackson JsonNode. There are two ways in which a field inside a JsonNode can be null. The first way is, if the field is completely missing from the JSON from which the JsonNode was created.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java json › modify jsonnode with java jackson
Modify JsonNode with Java Jackson - Apps Developer Blog
August 12, 2022 - To see where to find Jackson jars, read Introduction to Java JSON. The JsonNode class is immutable. That means we cannot modify it. For that purpose, we can use the ObjectNode, which is the subclass of the JsonNode. In this lesson, we will cover adding and removing a field from the JsonNode. Since we can not modify the JsonNode, we first need to cast it to the ObjectNode. Let’s create an empty ...
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › node › NullNode.html
NullNode (Jackson JSON Processor) - FasterXML
Method that can be used to check if this node was created from Json liternal null value. Overrides: isNull in class JsonNode · public String asText() Description copied from class: JsonNode · Method that will return valid String representation of the container value, if the node is a value node (method JsonNode.isValueNode() returns true), otherwise empty String.
Find elsewhere
🌐
GitHub
gist.github.com › diegoicosta › f06f61720f02b8c00afddb146ade5cce
Using Jackson to create manually a JsonNode · GitHub
Using Jackson to create manually a JsonNode. GitHub Gist: instantly share code, notes, and snippets.
🌐
Attacomsian
attacomsian.com › blog › jackson-json-node-tree-model
Working with Tree Model Nodes in Jackson
October 14, 2022 - // JSON string String json = "{\"id\":1,\"name\":\"John Doe\"}"; // create object mapper instance ObjectMapper mapper = new ObjectMapper(); // convert JSON string to `JsonNode` JsonNode node = mapper.readTree(json); Read convert JSON String to JsonNode using Jackson tutorial for more details.
🌐
Unity
forum.unity.com › unity engine
How to create a empty JSONNode? - Unity Engine - Unity Discussions
August 18, 2019 - I’m having problems figuring out how to create a empty JSON Node If i give it a parse empty bracket string like this it works. Gotta be another way? JSONNode m_JSONNode = JSON.Parse("{}");
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 2204
Add `JsonNode.isEmpty()` as convenience alias · Issue #2204 · FasterXML/jackson-databind
December 11, 2018 - An often-used idiom for checking for empty Arrays and Object for JsonNode is: if (arrayNode.size() == 0) { // is empty } and due to common use, would make sense to allow use via alias if (arrayNode.isEmpty()) { ... } Semantically it make...
Author   cowtowncoder
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.8 › com › fasterxml › jackson › databind › node › ObjectNode.html
ObjectNode (jackson-databind 2.8.0 API)
NOTE: Unlike all put(...) methods, return value is NOT this ObjectNode, but the newly created ObjectNode instance. ... Method for setting value of a field to specified numeric value. ... Alternative method that we need to avoid bumping into NPE issues with auto-unboxing. ... Method for setting value of a field to specified numeric value. The underlying JsonNode that will be added is constructed using JsonNodeFactory.numberNode(int), and may be "smaller" (like ShortNode) in cases where value fits within range of a smaller integral numeric value.
🌐
DEV Community
dev.to › sadiul_hakim › jackson-tutorial-comprehensive-guide-with-examples-2gdj
Jackson Tutorial: Comprehensive Guide with Examples - DEV Community
September 18, 2025 - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; ObjectMapper mapper = new ObjectMapper(); // Create an empty object node ObjectNode personNode = mapper.createObjectNode(); // Add properties to the object personNode.put("name", "John"); personNode.put("age", 30); personNode.put("isStudent", false); // Create an array node ArrayNode hobbiesNode = mapper.createArrayNode(); hobbiesNode.add("Reading"); hobbiesNode.add("Swimming"); hobbiesNo
🌐
GitHub
github.com › codehaus › jackson › blob › master › src › java › org › codehaus › jackson › JsonNode.java
jackson/src/java/org/codehaus/jackson/JsonNode.java at master · codehaus/jackson
* {@link org.codehaus.jackson.node}, which is in 'mapper' jar · * (whereas this class is in 'core' jar, since it is declared as · * nominal type for operations in {@link ObjectCodec}) */ public abstract class JsonNode · implements Iterable<JsonNode> { protected final static List<JsonNode> NO_NODES = Collections.emptyList(); protected final static List<String> NO_STRINGS = Collections.emptyList(); ·
Author   codehaus
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.7 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.7.0 API)
comparator - Object called to compare two scalar JsonNode instances, and return either 0 (are equals) or non-zero (not equal) ... Method that will produce developer-readable representation of the node; which may or may not be as valid JSON. If you want valid JSON output (or output formatted using one of other Jackson supported data formats) make sure to use ObjectMapper or ObjectWriter to serialize an instance, for example:
🌐
Javadoc.io
javadoc.io › doc › com.fasterxml.jackson.core › jackson-databind › 2.0.6 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode - jackson-databind 2.0.6 javadoc
Bookmarks · Latest version of com.fasterxml.jackson.core:jackson-databind · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind · Current version 2.0.6 · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.0.6 · package-list path (used for javadoc generation ...
Top answer
1 of 4
2

I figured out that this behaviour can be achieved via configuration. Here is the kotlin code but it's simple to convert to java Just create xmlMapper with appropriate configuration

    fun jacksonCreateXmlMapper(): XmlMapper {
        val module = JacksonXmlModule()
        module.setXMLTextElementName("value")
        return XmlMapper(module)
    }

For input

<products>
    <product count="5">apple</product>
    <product count="10">orange</product>
</products>

you get:

{
  "product" : [ {
    "count" : "5",
    "value" : "apple"
  }, {
    "count" : "10",
    "value" : "orange"
  } ]
}
2 of 4
1

You also could simply post-process the JSON DOM, traverse to all objects, and rename the keys that are empty strings to "value".

Race condition: such a key may already exist, and must not be overwritten
(e.g. <id type="pid" value="existing">abcdef123</id>).

Usage:
(note: you should not silently suppress the exception and return null, but allow it to propagate so the caller can decide to catch and apply failover logic if required)

public InputStream parseXmlResponse(InputStream xmlStream) throws IOException {
    JsonNode node = xmlMapper.readTree(xmlStream);
    postprocess(node);
    return new ByteArrayInputStream(jsonMapper.writer().writeValueAsBytes(node));
}

Post-processing:

private void postprocess(JsonNode jsonNode) {

    if (jsonNode.isArray()) {
        ArrayNode array = (ArrayNode) jsonNode;
        Iterable<JsonNode> elements = () -> array.elements();

        // recursive post-processing
        for (JsonNode element : elements) {
            postprocess(element);
        }
    }
    if (jsonNode.isObject()) {
        ObjectNode object = (ObjectNode) jsonNode;
        Iterable<String> fieldNames = () -> object.fieldNames();

        // recursive post-processing
        for (String fieldName : fieldNames) {
            postprocess(object.get(fieldName));
        }
        // check if an attribute with empty string key exists, and rename it to 'value',
        // unless there already exists another non-null attribute named 'value' which
        // would be overwritten.
        JsonNode emptyKeyValue = object.get("");
        JsonNode existing = object.get("value");
        if (emptyKeyValue != null) {
            if (existing == null || existing.isNull()) {
                object.set("value", emptyKeyValue);
                object.remove("");
            } else {
                System.err.println("Skipping empty key value as a key named 'value' already exists.");
            }
        }
    }
}

Output: just as expected.

{
   "elementName": {
     "id": {
        "type": "pid",
        "value": "abcdef123"
     }
   },
}

EDIT: considerations on performance:

I did a test with a large XML file (enwikiquote-20200520-pages-articles-multistream.xml, en.wikiquote XML dump, 498.4 MB), 100 rounds, with following measured times (using deltas with System.nanoTime()):

  • average read time (File, SSD): 2870.96 ms
    (JsonNode node = xmlMapper.readTree(xmlStream);)
  • average postprocessing time: 0.04 ms
    (postprocess(node);)
  • average write time (memory): 0.31 ms
    (new ByteArrayInputStream(jsonMapper.writer().writeValueAsBytes(node));)

That's a fraction of a millisecond for an object tree build from a ~500 MB file - so performance is excellent and no concern.

🌐
Makeseleniumeasy
makeseleniumeasy.com › 2020 › 05 › 16 › rest-assured-tutorial-27-how-to-create-json-array-using-jackson-api-objectmapper-createarraynode
REST Assured Tutorial 27 – How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()
// To empty JSON Array parentArray.removeAll(); System.out.println("After removing all elements from array : "+ objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parentArray)); package JacksonAPIUsage; import java.util.Arrays; import java.util.Iterator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; /* * [ { "firstname":"Jim", "lastname":"Brown", "totalprice":11