Compare with name and update that json element.

Resource resource = new ClassPathResource("gateway-fields.json");  //read from json file
      JsonFactory jsonFactory = new JsonFactory();
      ObjectMapper objectMapper = new ObjectMapper(jsonFactory);

      JsonNode arrayNode = objectMapper.readTree(resource.getFile()).get("fields");

      if (arrayNode.isArray()) {
          for (JsonNode jsonNode : arrayNode) {
              String nameFieldNode = jsonNode.get("name").asText();    
              if("RefNo".equals(nameFieldNode)){
                     ((ObjectNode)jsonNode).put("name", "1112");
              }
          }
      }
Answer from Kaustubh Khare on Stack Overflow
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 3120
Return `ListIterator` from `ArrayNode.elements()` · Issue #3120 · FasterXML/jackson-databind
April 16, 2021 - Iterator<Map.Entry<String, JsonNode>> ...(entry.getValue()) ); } However, for ArrayNode, there is no approach to selectively replace single JsonNodes while iterating over the content....
Author   ludgerb
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.node.arraynode
com.fasterxml.jackson.databind.node.ArrayNode.iterator java code examples | Tabnine
public static List<JsonNode> getAppModelReferencedProcessModels(JsonNode appModelJson) { List<JsonNode> result = new ArrayList<JsonNode>(); if (appModelJson.has("models")) { ArrayNode modelsArrayNode = (ArrayNode) appModelJson.get("models"); Iterator<JsonNode> modelArrayIterator = modelsArrayNode.iterator(); while (modelArrayIterator.hasNext()) { result.add(modelArrayIterator.next()); } } return result; } ... @Override public Set deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = (ObjectMapper) jp.getCodec(); JsonNode nod
🌐
Readthedocs
jse.readthedocs.io › en › latest › utils › jackson › jacksonJsonNodeForEach.html
Jackson JsonNode.forEach() with Java 8 Consumer — Java Repositories 1.0 documentation
Jackson has provided JsonNode.forEach() method which will accept Java 8 consumer definition to iterate each node. The consumer accepts only super classes of JsonNode.
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.node.arraynode
com.fasterxml.jackson.databind.node.ArrayNode.forEach java code examples | Tabnine
static List<URI> getRoles(JsonNode node) { final List<URI> roles = new ArrayList<>(); if (node != null && node.isArray()) { ((ArrayNode) node).forEach(e -> { roles.add(toUri(roleBase, e.asText())); }); } return roles; }
🌐
Baeldung
baeldung.com › home › json › jackson › simplified array operations on jsonnode without typecasting in jackson
Simplified Array Operations on JsonNode Without Typecasting in Jackson | Baeldung
June 27, 2025 - This approach reduces the overall complexity by directly iterating through the elements. It provides a straightforward mechanism for customizing the processing of JSON elements during iteration. In this tutorial, we explored various approaches to simplifying array operations on JsonNode without explicitly typecasting it to ArrayNode in Jackson.
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Simplified Json Array Operations with JsonNode in Jackson
May 6, 2024 - Java’s StreamSupport class can be leveraged to process elements in an ArrayNode: ArrayNode arrayNode = (ArrayNode) rootNode.get("myArray"); StreamSupport.stream(arrayNode.spliterator(), false) .forEach(element -> System.out.println(element.asText())); This method allows for convenient streaming and processing of JSON array elements. Iterating over elements in a JSON array without typecasting can be achieved using an Iterator:
🌐
Javadoc.io
javadoc.io › doc › com.fasterxml.jackson.core › jackson-databind › 2.9.7 › com › fasterxml › jackson › databind › node › ArrayNode.html
ArrayNode - jackson-databind 2.9.7 javadoc
Bookmarks · Latest version of com.fasterxml.jackson.core:jackson-databind · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind · Current version 2.9.7 · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.9.7 · package-list path (used for javadoc generation ...
Top answer
1 of 4
30

This will work for you :

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
Map<String, String> map = new HashMap<>();
addKeys("", root, map, new ArrayList<>());

map.entrySet()
     .forEach(System.out::println);

private void addKeys(String currentPath, JsonNode jsonNode, Map<String, String> map, List<Integer> suffix) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "-";

        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            addKeys(pathPrefix + entry.getKey(), entry.getValue(), map, suffix);
        }
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;

        for (int i = 0; i < arrayNode.size(); i++) {
            suffix.add(i + 1);
            addKeys(currentPath, arrayNode.get(i), map, suffix);

            if (i + 1 <arrayNode.size()){
                suffix.remove(arrayNode.size() - 1);
            }
        }

    } else if (jsonNode.isValueNode()) {
        if (currentPath.contains("-")) {
            for (int i = 0; i < suffix.size(); i++) {
                currentPath += "-" + suffix.get(i);
            }

            suffix = new ArrayList<>();
        }

        ValueNode valueNode = (ValueNode) jsonNode;
        map.put(currentPath, valueNode.asText());
    }
}

For the json you gave the output will be :

name-items-name-1-2=2nditem
name-items-name-1-1=firstitem
name-items-stock-1-1=12
name-first-1=John
name-items-stock-1-2=23
company=John Company
name-last-1=Doe
2 of 4
24

elements() gives you an iterator for subnodes and fields() gives you the properties.

With that, you can code a recursive function that walks through all nodes.

🌐
Java Tips
javatips.net › api › com.fasterxml.jackson.databind.node.arraynode
Java Examples for com.fasterxml.jackson.databind.node.ArrayNode
@Override public Actions deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException { ObjectMapper mapper = JSONUtils.OBJECT_MAPPER; JsonNode node = mapper.readTree(jp); if (node.isArray()) { return mapper.readValue(node.toString(), ActionsImpl.class); } else { ArrayNode array = mapper.createArrayNode(); array.add(node); return mapper.readValue(array.toString(), ActionsImpl.class); } } ... public void flattenJsonIntoMap(String currentPath, JsonNode jsonNode, Map<String, Object> map) { if (jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields(); String pathPrefix = currentPath.isEmpty() ?
🌐
ConcretePage
concretepage.com › jackson-api › jackson-jsonnode-foreach-with-java-8-consumer
Jackson JsonNode.forEach() with Java 8 Consumer
March 2, 2015 - Jackson API Jackson has provided JsonNode.forEach() method which will accept Java 8 consumer definition to iterate each node. The consumer accepts only super classes of JsonNode.
🌐
javathinking
javathinking.com › blog › convert-arraynode-to-list-java
Converting ArrayNode to List in Java — javathinking.com
For example, for an ArrayNode of integers, you can use the asInt method to get the integer value and add it to a List of integers. A: You can handle this by checking the type of each JsonNode using methods like isTextual, isNumber, etc.
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › node › ArrayNode.html
ArrayNode (Jackson JSON Processor)
For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), null is returned; ...
🌐
Medium
medium.com › @reham.muzzamil_67114 › a-quick-guide-to-iterate-over-a-dynamic-json-string-6b024aa6b1e
A quick guide to Iterate over a dynamic JSON string! | by Reham Muzzamil | Medium
May 16, 2020 - } else if (JsonNodeType.NULL == node.getNodeType()) { return null; } else { return node; } } else if (node.isNull()) { return null; } else if (node.isArray()) { ArrayNode arrayNode = (ArrayNode) node; ArrayNode cleanedNewArrayNode = mapper.createArrayNode(); for (JsonNode jsonNode : arrayNode) { cleanedNewArrayNode.add(traverse(jsonNode)); } return cleanedNewArrayNode; } else { ObjectNode encodedObjectNode = mapper.createObjectNode(); for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) { Map.Entry<String, JsonNode> entry = it.next(); encodedObjectNode.set(StringUtils.encodeValue(entry.getKey()), traverse(entry.getValue())); } return encodedObjectNode; } }
🌐
Whitfin
whitfin.io › blog › collecting-a-java-8-stream-into-a-jackson-arraynode
Whitfin's Blog: Java 8 Custom Collectors: Jackson ArrayNode
Ok, so now your IDE is going to yell at you about implementing the interface methods, so let's go ahead and add them in as shells. For learning purposes I've added comments to the shell to attempt to define what each one does. Note that I have omitted return values below, so this won't compile at this point. public class ArrayNodeCollector implements Collector<JsonNode, ArrayNode, ArrayNode> { @Override public Supplier<ArrayNode> supplier() { // This provides a Function which creates // a new instance of the accumulation type.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java json › iterate over jsonnode in java
Iterate over JsonNode in Java - Apps Developer Blog
August 12, 2022 - Here is an example program that iterates through the fields of a JsonNode and prints the field names and values: import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Iterator; import java.util.Map; class User { private String name; private String city; private String state; // constructors, getters, setters and toString() method } class Test { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); String json = "{\"key1\":\"value1\",\"user\":{\"name\":\"Steve\",\"city\":\"Paris\