# How to create a JSON Object using Jackson

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

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

In [an earlier article](https://attacomsian.com/blog/jackson-create-json-array), I wrote about how to create a JSON array using [Jackson API](https://attacomsian.com/blog/jackson-read-write-json). In this short article, you'll **learn to create a JSON object** using the `ObjectMapper` class from Jackson.

## 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 Object

The following example shows how you can use the `ObjectMapper` class to create a JSON object in Java:

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

    // create a JSON object
    ObjectNode user = mapper.createObjectNode();
    user.put("id", 1);
    user.put("name", "John Doe");
    user.put("email", "john.doe@example.com");
    user.put("salary", 3545.99);
    user.put("role", "QA Engineer");
    user.put("admin", false);

    // convert `ObjectNode` to pretty-print JSON
    // without pretty-print, use `user.toString()` method
    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);

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

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

The above code produces the following JSON object:

```json
{
  "id" : 1,
  "name" : "John Doe",
  "email" : "john.doe@example.com",
  "salary" : 3545.99,
  "role" : "QA Engineer",
  "admin" : false
}
```

## Create a Nested JSON Object

Just like a simple JSON object, you can also use the `ObjectMapper` class to create a JSON object inside another JSON object using Jackson API, as shown below:

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

    // create a JSON object
    ObjectNode user = mapper.createObjectNode();
    user.put("id", 1);
    user.put("name", "John Doe");
    user.put("email", "john.doe@example.com");
    user.put("salary", 3545.99);
    user.put("role", "QA Engineer");
    user.put("admin", false);

    // create a child JSON object
    ObjectNode address = mapper.createObjectNode();
    address.put("street", "2389  Radford Street");
    address.put("city", "Horton");
    address.put("state", "KS");
    address.put("zipCode", 66439);

    // append address to user
    user.set("address", address);

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

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

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

The above code generates the following JSON object:

```json
{
  "id" : 1,
  "name" : "John Doe",
  "email" : "john.doe@example.com",
  "salary" : 3545.99,
  "role" : "QA Engineer",
  "admin" : false,
  "address" : {
    "street" : "2389  Radford Street",
    "city" : "Horton",
    "state" : "KS",
    "zipCode" : 66439
  }
}
```

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.
