Assuming that a row is one of the entries in a ArrayNode the following simple approach may be useful. It uses the JsonNode abstraction instead of a series of nested Map objects which I personally prefer since JsonNode provides a series of utility methods that are helpful when dealing with this kind of data (data where the structure is possibly unknown or very dynamic so that it can't be easily transformed to a POJO).

The testcase below illustrates how to find the number of rows and how to print the values. To get hold of the values the method JsonNode.elements() is used and the number of rows is simply a call to the size()-method.

public class ArrayNodeTest {
    @Test
    public void verifySizeAndPrintRows() throws IOException {
        final String jsonStr =
                "[{\"key11\":\"value11\",\"key12\":\"value12\"},\n" +
                        "{\"key21\":\"value21\",\"key22\":\"value22\"},\n" +
                        "{\"keyn1\":\"valuen1\",\"keyn2\":\"valuen2\"}]";

        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode jsonNode = mapper.readTree(jsonStr);

        // Verify size
        Assert.assertEquals(3, jsonNode.size());

        // And print rows
        for (final JsonNode row : jsonNode) {
            final Iterable<JsonNode> iterable = () -> row.elements();
            iterable.forEach(elem -> System.out.println(elem.asText()));
        }
    }
}
Answer from wassgren on Stack Overflow
🌐
Tabnine
tabnine.com › home page › code › java › com.fasterxml.jackson.databind.node.arraynode
com.fasterxml.jackson.databind.node.ArrayNode.toString java code examples | Tabnine
@GET @Produces(MediaType.APPLICATION_JSON) @Path("/schemas") @ApiOperation(value = "List all schema names", notes = "Lists all schema names") public String listSchemaNames() { List<String> schemaNames = _pinotHelixResourceManager.getSchemaNames(); ArrayNode ret = JsonUtils.newArrayNode(); if (schemaNames != null) { for (String schema : schemaNames) { ret.add(schema); } } return ret.toString(); }
Top answer
1 of 2
5

Assuming that a row is one of the entries in a ArrayNode the following simple approach may be useful. It uses the JsonNode abstraction instead of a series of nested Map objects which I personally prefer since JsonNode provides a series of utility methods that are helpful when dealing with this kind of data (data where the structure is possibly unknown or very dynamic so that it can't be easily transformed to a POJO).

The testcase below illustrates how to find the number of rows and how to print the values. To get hold of the values the method JsonNode.elements() is used and the number of rows is simply a call to the size()-method.

public class ArrayNodeTest {
    @Test
    public void verifySizeAndPrintRows() throws IOException {
        final String jsonStr =
                "[{\"key11\":\"value11\",\"key12\":\"value12\"},\n" +
                        "{\"key21\":\"value21\",\"key22\":\"value22\"},\n" +
                        "{\"keyn1\":\"valuen1\",\"keyn2\":\"valuen2\"}]";

        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode jsonNode = mapper.readTree(jsonStr);

        // Verify size
        Assert.assertEquals(3, jsonNode.size());

        // And print rows
        for (final JsonNode row : jsonNode) {
            final Iterable<JsonNode> iterable = () -> row.elements();
            iterable.forEach(elem -> System.out.println(elem.asText()));
        }
    }
}
2 of 2
0

to identify the number of rows

What's a "row"? Is a key/value pair a row? Is an element of the JSON array (no matter how many key/value pairs it contains) a row?

print only the values on the screen

Jackson has nothing to do with printing anything on any screens. Jackson can be used to populate a Java data structure from the input JSON, and the populated Java data structure can then be used however you want.

Given the JSON structure in the original question, a simple solution would be to bind to a list (or array) of maps, and then just iterate through the list of maps, accessing all of the values. Following is such an example.

import java.io.File;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    Map<String, String>[] maps = mapper.readValue(new File("input.json"), Map[].class);
    for (Map<String, String> map : maps)
    {
      for (Map.Entry<String, String> entry : map.entrySet())
      {
        System.out.println(entry.getValue());
      }
    }
  }
}

Output:

value11
value12
value21
value22
valuen1
valuen2
🌐
Java Tips
javatips.net › api › org.codehaus.jackson.node.arraynode
Java Examples for org.codehaus.jackson.node.ArrayNode
The following java examples will help you to understand the usage of org.codehaus.jackson.node.ArrayNode. These source code samples are taken from different open source projects. ... public String toJson(List projects) { StringWriter out = new StringWriter(); try { JsonGenerator g = factory.createJsonGenerator(out); JsonNode root = mapper.createObjectNode(); ArrayNode projectArray = ((ObjectNode) root).putArray("projects"); for (Object project : projects) { projectArray.add(project.toString()); } mapper.writeValue(g, root); g.close(); } catch (IOException e) { throw new RuntimeException("Json parsing failed!
🌐
Baeldung
baeldung.com › home › json › jackson › difference between astext() and tostring() in jsonnode
Difference Between asText() and toString() in JsonNode | Baeldung
April 7, 2025 - The toString() method is overridden from the Object and returns a String representation of the JsonNode‘s data. This means that if we perform this operation, it’ll return the JSON’s text representation (including child nodes in the case ...
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › 6-5 › javadoc › com › fasterxml › jackson › databind › node › ArrayNode.html
ArrayNode (The Adobe AEM Quickstart and Web Application.)
This array node, to allow chaining · Since: 2.9 · public ArrayNode add(java.lang.String v) Method for adding specified String value at the end of this array. Returns: This array node, to allow chaining · public ArrayNode add(boolean v) Method for adding specified boolean value at the end of this array.
🌐
Medium
medium.com › @lakshmanaselvan252 › explain-about-the-jsonnode-and-arraynode-in-spring-boot-17f81c3de915
Explain About The JsonNode And ArrayNode In Spring Boot | by Lakshmana Selvan V | Medium
August 21, 2024 - Parsing JSON: The objectMapper.readTree(jsonString) method parses the JSON string into a JsonNode object. This JsonNode represents the entire JSON structure as a tree. ... ArrayNode is a subclass of JsonNode specifically designed to represent JSON arrays. It allows you to work with JSON arrays similarly to how you would work with List in Java.
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Simplified Json Array Operations with JsonNode in Jackson
May 6, 2024 - Let’s dive into these methods step by step to enhance your JSON manipulation skills in Java. Jackson is a popular Java library used for JSON processing. JsonNode is a fundamental abstraction representing a node in the JSON tree structure, capable of representing various JSON types such as objects, arrays, strings, numbers, and more. When working with JSON arrays ([]), the ArrayNode class in Jackson is especially useful for handling Simplified Array Operations on JsonNode in Jackson, enabling efficient manipulation and traversal of JSON arrays within Java applications.
🌐
DEV Community
dev.to › abharangupta › dive-into-jackson-for-json-in-java-understanding-jsonnode-arraynode-and-objectmapper-30g4
Dive into Jackson for JSON in Java: Understanding JsonNode, ArrayNode, and ObjectMapper - DEV Community
October 22, 2024 - With ArrayNode, Jackson makes handling JSON arrays a breeze! Now, let's talk about ObjectMapper - the heart and soul of Jackson. It's your go-to tool for converting Java objects to JSON and vice versa. Serializing Java Objects to JSON Serialization is just a fancy way of saying, "turn my Java object into a JSON string...
Find elsewhere
🌐
Cowtowncoder
cowtowncoder.com › hatchery › jackson › 0.9.2 › javadoc › org › codehaus › jackson › map › impl › ArrayNode.html
org.codehaus.jackson.map.impl Class ArrayNode
Let's mark this standard method as abstract to ensure all implementation classes define it · Specified by: equals in class JsonNode · public java.lang.String toString() Description copied from class: JsonNode · Let's mark this standard method as abstract to ensure all implementation classes define it ·
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 1.9 › org › codehaus › jackson › node › ArrayNode.html
ArrayNode (Jackson JSON Processor)
arrayNode, asText, binaryNode, binaryNode, booleanNode, getValueAsText, isContainerNode, nullNode, numberNode, numberNode, numberNode, numberNode, numberNode, numberNode, numberNode, objectNode, POJONode, textNode
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.6 › com › fasterxml › jackson › databind › node › ArrayNode.html
ArrayNode (jackson-databind 2.6.0 API)
Method that will insert specified String at specified position in this array. ... Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
🌐
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); } } } } "Java" "Scala" "Python" raja ·
🌐
Attacomsian
attacomsian.com › blog › jackson-create-json-array
How to create a JSON array using Jackson
October 14, 2022 - try { // create `ObjectMapper` instance ObjectMapper mapper = new ObjectMapper(); // create three JSON objects ObjectNode user1 = mapper.createObjectNode(); user1.put("id", 1); user1.put("name", "John Doe"); ObjectNode user2 = mapper.createObjectNode(); user2.put("id", 2); user2.put("name", "Tom Doe"); ObjectNode user3 = mapper.createObjectNode(); user3.put("id", 3); user3.put("name", "Emma Doe"); // create `ArrayNode` object ArrayNode arrayNode = mapper.createArrayNode(); // add JSON users to array arrayNode.addAll(Arrays.asList(user1, user2, user3)); // convert `ArrayNode` to pretty-print JSON // without pretty-print, use `arrayNode.toString()` method String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode); // print json System.out.println(json); } catch (Exception ex) { ex.printStackTrace(); }
🌐
Fasterxml
fasterxml.github.io › jackson-databind › javadoc › 2.7 › com › fasterxml › jackson › databind › node › ArrayNode.html
ArrayNode (jackson-databind 2.7.0 API)
Method that will insert specified String at specified position in this array. ... Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.
🌐
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 - In this tutorial, we’ll еxplorе different approaches to simplifying array operations on a JsonNodе without explicitly casting it to ArrayNode in Java. This is necessary when manipulating the data directly in our code. JsonNode is an abstract class in the Jackson library that represents a node in the JSON tree. It’s the base class for all nodes and is capable of storing different types of data, including objects, arrays, strings, numbers, booleans, and null values.
🌐
Smithy
smithy.io › javadoc › 1.21.0 › software › amazon › smithy › model › node › ArrayNode.html
ArrayNode (Smithy API 1.21.0)
public <T,K extends Node> java.util.List<T> getElementsAs(java.util.function.Function<K,T> f) Gets the elements of the ArrayNode as a specific type by applying a mapping function to each node. Each Node is cast to K and then mapped with the provided function to return type T. ArrayNode array ...