# How to create a JSON array using Jackson

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 26, 2019, updated Oct 14, 2022. Topics: Java, JSON, Jackson.
Canonical URL: https://attacomsian.com/blog/jackson-create-json-array

> A short tutorial to learn how to create a JSON array using Jackson API.

[Jackson](https://attacomsian.com/blog/jackson-read-write-json) 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](https://attacomsian.com/blog/install-gradle-on-ubuntu) project, add the following dependency to the `build.gradle` file:

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

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

```xml
<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:

```java
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](https://attacomsian.com/blog/jackson-pretty-print-json-node-to-string) array:

```json
[ {
  "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:

```java
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:

```json
[ {
  "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](https://attacomsian.com/blog/jackson-read-write-json) tutorial.
