Gson – How to enable pretty print JSON output

In this tutorial, we will show you how to enable JSON pretty print in Gson framework.

1. By default, Gson compact-print the JSON output :

GsonExample1.java

package com.mkyong;

import com.google.gson.Gson;

public class GsonExample1 {

    public static void main(String[] args) {

        Gson gson = new Gson();

        String[] lang = {"Java", "Node", "Kotlin", "JavaScript"};

        String json = gson.toJson(lang);

        System.out.println(json);

    }

}

Output


["Java","Node","Kotlin","JavaScript"]

2. To enable JSON pretty-print, create Gson object with GsonBuilder

GsonExample2.java

package com.mkyong;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonExample2 {

    public static void main(String[] args) {

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String[] lang = {"Java", "Node", "Kotlin", "JavaScript"};

        String json = gson.toJson(lang);

        System.out.println(json);

    }

}

Output


[
  "Java",
  "Node",
  "Kotlin",
  "JavaScript"
]
Note
Read more Gson examples

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
mkyongpavansngOrlando PYmkyong Recent comment authors
newest oldest most voted
sng
Guest
sng

Using gson.toJson(someObj) and new GsonBuilder().setPrettyPrinting().create() does not provide any difference result to me. My both the results are something like “{“name”:”mkyong”,”age”:35,”position”:”Founder”,”salary”:10000,”skills”:[“java”,”python”,”shell”]}”. Could some one help me in getting the correct format.

pavan
Guest
pavan

HI ,

can you pls tell me , how to compare two json responses or files in java

Orlando PY
Guest
Orlando PY

Muchas Gracias!! Me fue muy Util!!
Thanks!! it was very usefull for me

GOWRI SANKAR
Guest
GOWRI SANKAR

Nice utility. Thanks for sharing!

Peter McKenna
Guest
Peter McKenna

Very cool! There’s online tools like http://www.jsonprettyprint.net too where you can copy paste your raw JSON to pretty print it.

mkyong
Guest
mkyong

Simple and nice. thanks for sharing.

Ram
Guest
Ram

Really You are the one for Java Real way of explanation

Boris
Guest
Boris

Thank you. This saved me some time.

trackback
??Gson?????Json?? - K-Res?Blog | K-Res?Blog

[…]  How To Enable Pretty Print JSON Output (Gson) //www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/ […]

trackback
Java JSON Tutorial

[…] Streaming example Example to use JsonWriter() and JsonReader () to stream read and write JSON data.Pretty print JSON output in Gson Pretty print JSON output via GsonBuilder.JSON.simpleSimple Java library for JSON, to read and write […]

trackback
How to convert Java object to / from JSON (Gson)

[…] list=[String 1, String 2, String 3]] Pretty Print JSON You may interest at this – How to enable pretty print JSON output ReferencesGoogle Gson – http://code.google.com/p/google-gson/Json Official site – […]