If you want to explicitly traverse through the json and find the value of a , you can do it like this for the json that you specified.

String aValue = jsonNode.get("text").get(0).get("a").asText();

Finding the value of b would be

String bValue = jsonNode.get("text").get(1).get("b").asText();

You can also traverse through the elements within the text array and get the values of a and b as

for (JsonNode node : jsonNode.get("text")) {
    System.out.println(node.fields().next().getValue().asText());
}

And that would print the below on the console

1
2
Answer from Madhu Bhat 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 - Finally, we want to iterate over all the child nodes of each Object node. One option is to use JsonNode.elements.
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.7 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.7.0 API)
Same as calling elements(); implemented ... over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> ...
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.jsonnode
com.fasterxml.jackson.databind.JsonNode.elements java code examples | Tabnine
private static List<JmesPathExpression> getChildren(JsonNode jsonNode){ if(jsonNode.get("children").size() < 1) { throw new RuntimeException("Expected one or more arguments"); } Iterator<JsonNode> children = jsonNode.get("children").elements(); final List<JmesPathExpression> childrenList = new ArrayList<>(); while (children.hasNext()) { childrenList.add(fromAstJsonToAstJava(children.next())); } return childrenList; }
🌐
Red Hat
access.redhat.com › webassets › avalon › d › red-hat-jboss-enterprise-application-platform › 7.1.beta › javadocs › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (Red Hat JBoss Enterprise Application Platform 7.1.0.Beta1 public API)
Same as calling elements(); implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> fields()
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › cloud-service › javadoc › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (The Adobe Experience Manager SDK 2022.11.9850.20221116T162329Z-220900)
public java.util.Iterator<JsonNode> elements() Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator.
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › 6-4 › javadoc › com › fasterxml › jackson › databind › JsonNode.html
JsonNode ("The Adobe AEM Quickstart and Web Application.")
public java.util.Iterator<JsonNode> elements() Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator.
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.4 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.4.0 API)
Same as calling elements(); implemented ... over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › jackson › jackson_tree_model.htm
Jackson - Tree Model
JsonNode nameNode = rootNode.path("name"); System.out.println("Name: "+ nameNode.textValue()); JsonNode marksNode = rootNode.path("marks"); Iterator<JsonNode> iterator = marksNode.elements();
Top answer
1 of 3
89

Acquire an ObjectReader with ObjectMapper#readerFor(TypeReference) using a TypeReference describing the typed collection you want. Then use ObjectReader#readValue(JsonNode) to parse the JsonNode (presumably an ArrayNode).

For example, to get a List<String> out of a JSON array containing only JSON strings

CopyObjectMapper mapper = new ObjectMapper();
// example JsonNode
JsonNode arrayNode = mapper.createArrayNode().add("one").add("two");
// acquire reader for the right type
ObjectReader reader = mapper.readerFor(new TypeReference<List<String>>() {
});
// use it
List<String> list = reader.readValue(arrayNode);
2 of 3
13

If an Iterator is more useful...

...you can also use the elements() method of ArrayNode. Example see below.

sample.json

Copy{
    "first": [
        "Some string ...",
        "Some string ..."
    ],
    "second": [
        "Some string ..."
    ]
}

So, the List<String> is inside one of the JsonNodes.

Java

When you convert that inner node to an ArrayNode you can use the elements() method, which returns an Iterator of JsonNodes.

CopyFile file = new File("src/test/resources/sample.json");
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(file);
ArrayNode arrayNode = (ArrayNode) jsonNode.get("first");
Iterator<JsonNode> itr = arrayNode.elements();
// and to get the string from one of the elements, use for example...
itr.next().asText();

New to Jackson Object Mapper?

I like this tutorial: https://www.baeldung.com/jackson-object-mapper-tutorial

Update:

You can also use .iterator() method of ArrayNode. It is the same:

Same as calling .elements(); implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs.

from the javadocs of com.fasterxml.jackson.core:jackson-databind:2.11.0

🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.9 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.9.0 API)
Same as calling elements(); implemented ... over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> ...
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-jsonnode.html
Jackson JsonNode
A JsonNode that represents a JSON object or JSON array can be traversed like any other object graph. You do so by iterating its nested fields (or nested elements in case of an array).
🌐
Baeldung
baeldung.com › home › json › jackson › get all the keys in a json string using jsonnode
Get all the Keys in a JSON String Using JsonNode | Baeldung
May 11, 2024 - In this tutorial, we’ll explore different ways to extract all the nested keys from a JSON using JsonNode.
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.8 › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (jackson-databind 2.8.0 API)
Same as calling elements(); implemented ... over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> ...
🌐
Red Hat
access.redhat.com › webassets › avalon › d › jboss_enterprise_application_platform_continuous_delivery › 19 › javadocs › com › fasterxml › jackson › databind › JsonNode.html
JsonNode (Red Hat JBoss Enterprise Application Platform 7.4.0.CD19 public API)
Same as calling elements(); implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs. ... Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. public Iterator<Map.Entry<String,JsonNode>> fields()
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2020 › 09 › 16 › rest-assured-tutorial-46-fetch-value-from-json-array-using-jsonnode-jackson-get-path-methods
REST Assured Tutorial 46 – Fetch Value From JSON Array Using JsonNode – Jackson – Get() & Path() Methods
We can get the value of a node using get() and path() methods of JsonNode class. We need to extract value with appropriate data types after using get() and path() methods. We just need to use an index to fetch an element of an array which is the core concept of an array.