How to pretty print JsonNode to JSON string using Jackson

In this short article, you will learn how to pretty-print a JsonNode object to a JSON string using the Jackson library.

Pretty print JsonNode using toPrettyString()

The simplest and straightforward way to pretty print JsonNode is using the toPrettyString() method, as shown below:

try {
    // JSON string
    String json = "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
            "\"roles\":[\"Member\",\"Admin\"],\"admin\":true,\"city\"" +
            ":\"New York City\",\"country\":\"United States\"}";

    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON string to `JsonNode`
    JsonNode node = mapper.readTree(json);

    // `JsonNode` to JSON string
    String prettyString = node.toPrettyString();

    // print pretty JSON string
    System.out.println(prettyString);

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

The above example code will output the following pretty print JSON:

{
  "name" : "John Doe",
  "email" : "john.doe@example.com",
  "roles" : [ "Member", "Admin" ],
  "admin" : true,
  "city" : "New York City",
  "country" : "United States"
}

Pretty print JsonNode using writerWithDefaultPrettyPrinter()

Another way of pretty printing JsonNode is to use the ObjectMapper class writerWithDefaultPrettyPrinter() method:

try {
    // JSON string
    String json = "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
            "\"roles\":[\"Member\",\"Admin\"],\"admin\":true,\"city\"" +
            ":\"New York City\",\"country\":\"United States\"}";

    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON string to `JsonNode`
    JsonNode node = mapper.readTree(json);

    // `JsonNode` to JSON string
    String prettyString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);

    // print pretty JSON string
    System.out.println(prettyString);

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

Here is the output of the above example code:

{
  "name" : "John Doe",
  "email" : "john.doe@example.com",
  "roles" : [ "Member", "Admin" ],
  "admin" : true,
  "city" : "New York City",
  "country" : "United States"
}

Read the guide Working with Tree Model Nodes in Jackson for more JsonNode examples.

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.