# How to pretty print JsonNode to JSON string using Jackson

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 17, 2019, updated Oct 14, 2022. Topics: Java, JSON, Jackson.
Canonical URL: https://attacomsian.com/blog/jackson-pretty-print-json-node-to-string

> A short guide to learn how to pretty print a JsonNode to a 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](https://attacomsian.com/blog/jackson-read-write-json) library.

## Pretty print `JsonNode` using `toPrettyString()`

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

```java
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:

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

```java
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:

```java
{
  "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](https://attacomsian.com/blog/jackson-json-node-tree-model) for more `JsonNode` examples.

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.
