In an earlier article, I wrote about how to create a JSON array using Jackson API. 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 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 Object

The following example shows how you can use the ObjectMapper class to create a JSON object in 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:

{
  "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:

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:

{
  "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 tutorial.

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