Yes, the Jackson manual parser design is quite different from other libraries. In particular, you will notice that JsonNode has most of the functions that you would typically associate with array nodes from other APIs. As such, you do not need to cast to an ArrayNode to use. Here's an example:

JSON:

{
    "objects" : ["One", "Two", "Three"]
}

Code:

final String json = "{\"objects\" : [\"One\", \"Two\", \"Three\"]}";

final JsonNode arrNode = new ObjectMapper().readTree(json).get("objects");
if (arrNode.isArray()) {
    for (final JsonNode objNode : arrNode) {
        System.out.println(objNode);
    }
}

Output:

"One"
"Two"
"Three"

Note the use of isArray to verify that the node is actually an array before iterating. The check is not necessary if you are absolutely confident in your data structure, but it's available should you need it (and this is no different from most other JSON libraries).

Answer from Perception on Stack Overflow
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.jsonnode
com.fasterxml.jackson.databind.JsonNode.isArray java code examples | Tabnine
List<String> getStringOrArray(... ... !node.asText().isEmpty()) { return Collections.singletonList(node.asText()); } List<String> list = new ArrayList<>(node.size()); ......
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

🌐
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 the above example, we can also use the fields() method of the JsonNode class to get field objects instead of just field names: public List<String> getAllKeysInJsonUsingJsonNodeFields(String json, ObjectMapper mapper) throws JsonMappingException, JsonProcessingException { List<String> keys = new ArrayList<>(); JsonNode jsonNode = mapper.readTree(json); getAllKeysUsingJsonNodeFields(jsonNode, keys); return keys; } private void getAllKeysUsingJsonNodeFields(JsonNode jsonNode, List<String> keys) { if (jsonNode.isObject()) { Iterator<Entry<String, JsonNode>> fields = jsonNode.fields(); fields.forEachRemaining(field -> { keys.add(field.getKey()); getAllKeysUsingJsonNodeFieldNames((JsonNode) field.getValue(), keys); }); } else if (jsonNode.isArray()) { ArrayNode arrayField = (ArrayNode) jsonNode; arrayField.forEach(node -> { getAllKeysUsingJsonNodeFieldNames(node, keys); }); } }
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › node › ArrayNode.html
ArrayNode (Jackson JSON Processor)
clone, finalize, getClass, notify, ... JsonToken asToken() Description copied from class: BaseJsonNode · Method that can be used for efficient type detection when using stream abstraction for traversing nodes....
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-jsonnode-to-arraynode-using-jackson-api-in-java
How to convert JsonNode to ArrayNode using Jackson API in Java?
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.core.JsonProcessingException; public class JSonNodeToArrayNodeTest { public static void main(String args[]) throws JsonProcessingException { String jsonStr = "{\"Technologies\" : [\"Java\", \"Scala\", \"Python\"]}"; ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = (ArrayNode) mapper.readTree(jsonStr).get("Technologies"); if(arrayNode.isArray()) { for(JsonNode jsonNode : arrayNode) { System.out.println(jsonNode); } } } }
Find elsewhere
🌐
Java Tips
javatips.net › api › org.codehaus.jackson.node.arraynode
Java Examples for org.codehaus.jackson.node.ArrayNode
@RequestMapping("/facebook/info") public String photos(Model model) throws Exception { ObjectNode result = facebookRestTemplate.getForObject("https://graph.facebook.com/me/friends", ObjectNode.class); ArrayNode data = (ArrayNode) result.get("data"); ArrayList<String> friends = new ArrayList<String>(); for (JsonNode dataNode : data) { friends.add(dataNode.get("name").getTextValue()); } model.addAttribute("friends", friends); return "facebook"; }
🌐
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 need to use class ObjectMapper provided by Jackson API. ObjectMapper class provides a method “readTree()” which is responsible to deserialize JSON content as tree expressed using a set of JsonNode instances. We can get the value of a node using get() and path() methods of JsonNode class.
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.node.arraynode
com.fasterxml.jackson.databind.node.ArrayNode.forEach java code examples | Tabnine
private static List<String> convertToList(ArrayNode values) { final List<String> result = new ArrayList<>(values.size()); values.forEach(jsonNode -> result.add(jsonNode.asText())); return result; } } ... static List<URI> getRoles(JsonNode node) ...
Top answer
1 of 2
1

Thanks for all the replies :)

I required the JsonNode coming via REST request containing key->value pairs to be converted into an ArrayList<OptionalHeader>, see the OptionalHeader class in question.

finally did it using following:

(getting it as JsonNode in my POJO) coming via REST request. I used below in my setter.

public void setOptHeaders(JsonNode optHeaders) throws JsonParseException, JsonMappingException, IOException {
    this.optHeaders = optHeaders;
    List<OptionalHeader> allHeaders = new ArrayList<OptionalHeader>();
    Iterator<Entry<String, JsonNode>> nodeIterator = optHeaders.fields();
    while (nodeIterator.hasNext()) {
       Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
       OptionalHeader header = new OptionalHeader(entry.getKey(),entry.getValue().asText());
       allHeaders.add(header);

    }
    optionalEmailHeaders.addAll(allHeaders);
}

then following in the getter to convert it back.

public JsonNode getOptHeaders() {
    final ObjectMapper mapper = new ObjectMapper();
    final Map<String, String> map = new HashMap<String, String>();
    for (final OptionalHeader data: optionalEmailHeaders)
        map.put(data.getName(), data.getValue());
    optHeaders = mapper.valueToTree(map);
    return optHeaders;
}
2 of 2
0

Jackson mapper is there which can map Java Objects from/to JSON objects. You can get the Jackson API from here. Try saving your JSON string into a file eg. 'headers.json'. Jackson provides a ObjectMapper class and you can use its readValue( File file, Class class) method to get the Java object from JSON like :-

ObjectMapper mapper = new ObjectMapper(); OptionalHeader optHeader= mapper.readValue(new File("c:\\headers.json"), OptionalHeader .class);

You can use the same link for getting the docs & read more about it.

🌐
Baeldung
baeldung.com › home › json › jackson › convert jackson jsonnode to typed collection
Convert Jackson JsonNode to Typed Collection | Baeldung
June 21, 2025 - To convert a JsonNode to a list, we can traverse it entry by entry and create a List object with it: List<Person> manualJsonNodeToList(JsonNode personsNode) { List<Person> people = new ArrayList<>(); for (JsonNode node : personsNode) { Person ...
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Simplified Json Array Operations with JsonNode in Jackson
May 6, 2024 - To access elements within a JSON array using JsonNode, the get() method is handy.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java json › convert json array to java list using jackson
Convert JSON Array to Java List using Jackson - Apps Developer Blog
March 22, 2024 - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; import java.util.List; public class ManualConversionExample { public static void main(String[] args) { String jsonArrayString = "[{\"id\": 1, \"name\": \"John\"}, {\"id\": 2, \"name\": \"Jane\"}]"; ObjectMapper objectMapper = new ObjectMapper(); try { JsonNode jsonArray = objectMapper.readTree(jsonArrayString); List<Person> personList = new ArrayList<>(); for (JsonNode element : jsonArray) { Person person = objectMapper.treeToValue(element, Person.class); personList.a