1. Overview

This article is about sping Jackson enable pretty Json or enable readable Json format. Spring boot provides default configurations with Jackson to convert Java POJO to Json. We can also use Gson as a library to convert data to Json format both have their own advantages. Here are more details about Spring Jackson and Gson configurations and viewpoints.

When Jackson convert to Json format at that time it was very difficult for us to read that format especially when large amount data are there so we need a proper spacing and readable format for debugging and tracing purposes.

2. Ways to enable Jackson pretty/readable Json

Here we will learn about two ways to configure pretty Json for Jackson, 1st one using Jackson properties and 2nd using Jackson Bean.

1. application.properties

spring.jackson.serialization.indent-output is a property that we can write in application.properties or application.yml file. when we set true value then it will show pretty Json and false means normal Json format.

spring.jackson.serialization.indent-output=true

2. Bean configuration

Here we can also enable or disable  SerializationFeature.INDENT_OUTPUT in ObjectMapper object.

public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        return new Jackson2ObjectMapperBuilder() {
            @Override
            public void configure(ObjectMapper objectMapper) {
                super.configure(objectMapper);
                objectMapper.enable(SerializationFeature.INDENT_OUTPUT);  // comment this line for disable pretty
            }
        };
}

Output:

Spring Jackson enable pretty Json - output.png

3. Concussion

In this article, we learned about how we can configure pretty Json with Jackson. We like to suggest that do not enable pretty json with production it may also increase the size of payload because of extra white space.

 

Was this post helpful?

1 comment. Leave new

Great

Leave a Reply

Your email address will not be published. Required fields are marked *