How to enable pretty print JSON output (Gson)

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

1. By default, Gson display the JSON output like the following :


	Gson gson = new Gson();
	String json = gson.toJson(someObj);
	System.out.println(json);

Output

{"name":"mkyong","age":35,"position":"Founder","salary":10000,"skills":["java","python","shell"]}

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


	Gson gson = new GsonBuilder().setPrettyPrinting().create();
	String json = gson.toJson(obj);
	System.out.println(json);

Output

{
  "name": "mkyong",
  "age": 35,
  "position": "Founder",
  "salary": 10000,
  "skills": [
    "java",
    "python",
    "shell"
  ]
}

3. Full example.

GsonExample.java

package com.mkyong.json;

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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class GsonExample {

    public static void main(String[] args) {

        Staff staff = createDummyObject();

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

        String json = gson.toJson(staff);
        System.out.println(json);
        
    }

    private static Staff createDummyObject() {

        Staff staff = new Staff();

        staff.setName("mkyong");
        staff.setAge(35);
        staff.setPosition("Founder");
        staff.setSalary(new BigDecimal("10000"));

        List<String> skills = new ArrayList<>();
        skills.add("java");
        skills.add("python");
        skills.add("shell");

        staff.setSkills(skills);

        return staff;

    }

}

Output

{
  "name": "mkyong",
  "age": 35,
  "position": "Founder",
  "salary": 10000,
  "skills": [
    "java",
    "python",
    "shell"
  ]
}

Reference

  1. GsonBuilder JavaDoc

About the Author

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

Comments

Leave a Reply

avatar
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.

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 – […]