Jackson - Enable Pretty Print JSON Output

In this quick tutorial, I show you how to use Jackson library to pretty print JSON Object to console or external file.

Dependencies

Let’s first add the following dependencies to the pom.xml:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
I found two ways we can configure and enable pretty print JSON with ObjectMapper class.
  1. Using ObjectMapper.writerWithDefaultPrettyPrinter() Method
  2. Using ObjectMapper.enable(SerializationFeature.INDENT_OUTPUT) Method

1. Using ObjectMapper.writerWithDefaultPrettyPrinter() Method

// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
Output:
{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

2. Using ObjectMapper.enable(SerializationFeature.INDENT_OUTPUT) Method

// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writeValueAsString(bean);
System.out.println(result);
Output:
{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

Complete Example

Let's develop a complete example. Let's first create a User class:

User.java

package net.javaguides.jackson.annotations;

public class User {

    public int id;

    private String firstName;

    private String lastName;

    private String fullName;

    public User(int id, String firstName, String lastName, String fullName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = fullName;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

Using ObjectMapper.writerWithDefaultPrettyPrinter() Method

package net.javaguides.jackson.annotations;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Demo {
    public static void main(String[] args) throws IOException {

        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
        String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);

        System.out.println(result);
    }
}
Output:
{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

Using ObjectMapper.enable(SerializationFeature.INDENT_OUTPUT) Method

package net.javaguides.jackson.annotations;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonPropertyAnnotationTest {
    public static void main(String[] args) throws IOException {

        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
        String result = mapper.writeValueAsString(bean);

        System.out.println(result);
    }
}
Output:
{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

Related Articles


GitHub Repository

The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorial

Comments