Skip to content

Gson Pretty Print JSON example

Gson Pretty Print JSON example shows how to set the output to the pretty print mode in Java. By default, the Gson prints output in a compact format that removes all whitespaces, and pretty print is disabled, but it can be enabled very easily.

How to enable pretty print output in Gson?

The default value of the pretty print output is disabled as we can see in the below given example.

Output

The output becomes unreadable when we are dealing with large data in the default compact output format. To enable the pretty print output, we need to create the Gson object using the GsonBuilder class instead of using the constructor of the Gson class.

We can use the setPrettyPrinting method of the GsonBuilder class to set it to true. Once we do that, we can create an object of the Gson class using the create method as given below.

Output

As you can see from the output, pretty printing is enabled now.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.