How to create a JSON Object using Jackson

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.