Jackson is a popular open-source library for reading, writing, and parsing JSON data in Java. In this short tutorial, you'll learn how to create a JSON array using Jackson API.

Dependencies

To add Jackson to your Gradle project, add the following dependency to the build.gradle file:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0'

For Maven, include the below dependency to your pom.xml file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
</dependency>

Create a JSON Array

The following example demonstrates how you can use the ObjectMapper class to produce a JSON array:

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();
}

The above code will produce the following pretty-print JSON array:

[ {
  "id" : 1,
  "name" : "John Doe"
}, {
  "id" : 2,
  "name" : "Tom Doe"
}, {
  "id" : 3,
  "name" : "Emma Doe"
} ]

Create a Nested JSON Array

Just like a simple JSON array, you can also create a JSON array inside another JSON array using Jackson:

try {
    // create `ObjectMapper` instance
    ObjectMapper mapper = new ObjectMapper();

    // create three JSON objects
    ObjectNode vendor1 = mapper.createObjectNode();
    vendor1.put("name", "Ford");

    ObjectNode vendor2 = mapper.createObjectNode();
    vendor2.put("name", "BMW");

    ObjectNode vendor3 = mapper.createObjectNode();
    vendor3.put("name", "Fiat");

    // create nested arrays
    ArrayNode models1 = mapper.createArrayNode();
    models1.add("Fiesta");
    models1.add("Focus");
    models1.add("Mustang");

    ArrayNode models2 = mapper.createArrayNode();
    models2.add("320");
    models2.add("X3");
    models2.add("X5");

    ArrayNode models3 = mapper.createArrayNode();
    models3.add("500");
    models3.add("Panda");

    // add nested arrays to JSON objects
    vendor1.set("models", models1);
    vendor2.set("models", models2);
    vendor3.set("models", models3);

    // create `ArrayNode` object
    ArrayNode arrayNode = mapper.createArrayNode();

    // add JSON objects to array
    arrayNode.addAll(Arrays.asList(vendor1, vendor2, vendor3));

    // convert `ArrayNode` to pretty-print JSON
    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode);

    // print json
    System.out.println(json);

} catch (Exception ex) {
    ex.printStackTrace();
}

The above code will generate the following JSON array:

[ {
  "name" : "Ford",
  "models" : [ "Fiesta", "Focus", "Mustang" ]
}, {
  "name" : "BMW",
  "models" : [ "320", "X3", "X5" ]
}, {
  "name" : "Fiat",
  "models" : [ "500", "Panda" ]
} ]

For more Jackson examples, check out the How to read and write JSON using Jackson in Java tutorial.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.