Enable Pretty Print of JSON with Jackson

Enable Pretty Print of JSON with Jackson

0 Comments

JSON has become the most preferred way of transmitting data across network connections. JSON being easily readable by machines is one of the many reasons for JSON’s popularity. However, JSON unless formatted well, is not easily readable by human. Pretty printing a JSON is one common operation to improve the readability of the JSON. Enabling pretty print for Jackson is easy when you are using Jackson on its own. Even easier when you are using Jackson with Spring Boot.

In this post, I will explain how to pretty print JSON using the Jackson library standalone, and the Spring configuration under Spring Boot.

The Maven POM

In order to use Jackson, you need the Jackson JAR files. If you are using Maven, include the Jackson dependencies in your Maven POM.

Here is the code to add the Jackson dependencies.

Note: When using Spring Boot, the Jackson dependencies are typically included under the Spring Boot starters.

The POJO

Let’s create a JsonPrettyPrintDemoBean POJO with few fields that will be serialized to JSON.

The JsonPrettyPrintDemoBean POJO is this.

JsonPrettyPrintDemoBean.java

Pretty Print JSON Object

Let us serialize the JsonPrettyPrintDemoBean POJO into JSON. A test class to test the serialization and print the serialized object is this.

The output of running the test is this.

As you can notice in the preceding output, the JSON is compact. It is not formatted and therefore difficult to read. Imagine such output for a large number of properties. Lack of proper formatting makes it difficult to read or search the JSON for any particular key or value. To address this challenge, you can pretty print JSON.

To enable pretty printing, you need to call the writerWithDefaultPrettyPrinter() on the ObjectMapper(), like this.

With pretty printing, the output JSON is this.

Pretty Print JSON String

Let’s try to use writerWithDefaultPrettyPrinter() with a JSON string with this code snippet.

The output printed is in compact mode and will be the same as the String input.

To enable pretty printing, read the JSON string into an object. In our example, we can read the jsonString into the JsonPrettyPrintDemoBean POJO. Then, enable pretty printing, with a call the writerWithDefaultPrettyPrinter() on the ObjectMapper, like this.

The output is this.

If you only need to pretty print a JSON string, you can use Object instead of creating an application POJO.

The code to do so is this.

The output with pretty print is this.

The complete test code is this.

JsonPrettyPrintDemoBeanTest.java

The output of running the test in IntelliJ is this.

Output of running the test class in IntelliJ

Pretty Printing Jackson with Spring Boot

A common use case for pretty printing JSON is in Spring RESTFul Web services. Such services expose endpoints that clients consume. Clients can be front-end applications using some HTTP library, a browser sending GET requests, or a REST client such as Postman.

These clients make REST calls to the service and receives JSON data. At times, the clients might require presenting the data to users. Without pretty printing, the JSON returned by a Spring Boot REST service in Postman looks like this.
Raw JSON from Jackson under Spring Boot in Postman

For a human-readable format, we need to enable JSON pretty printing.

With minimal changes you can enable pretty printing in your Spring Boot application.

The most convenient method is to enable pretty printing in the application.properties file, like this:

The second approach is to programmatically set the prettyPrint
field of MappingJackson2HttpMessageConverter to true. To do so, create a configuration class in your application like this.

JacksonPrettyPrintConfiguration.java

This configuration class extends WebMvcConfigurationSupport and overrides the extendMessageConverters() method. In this method, the setPrettyPrint() method is called on the MappingJackson2HttpMessageConverter object, passing true as the parameter.

With pretty printing enabled, the JSON output in Postman is this.
pretty print JSON from Jackson with Spring Boot

You can download the example code from here.

0

About jt

    You May Also Like

    Leave a Reply