Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Enable Pretty Print JSON Output using Gson example

In this example we are going to see how to enalbe pretty printing in JSON. As you might have noticed in the previous examples the JSON output of our programs was not properly aligned and thus somehow hard to read.

For this demo we are going to use a simple Java class thay we used in the “Convert Java Object To / From JSON using Gson” tutorial.


import java.util.ArrayList;
import java.util.List;

public class Student {

	private int id;
	private String firstName;
	private String lastName;
	private int age;

	private List<String> list;

	public Student(String fname, String lname, int age, int id) {

		this.firstName = fname;
		this.lastName = lname;
		this.age = age; = id;

		this.list = new ArrayList<String>();


	public void setFirstName(String fname) {
		this.firstName = fname;

	public String getFirstName() {
		return this.firstName;

	public void setLastName(String lname) {
		this.lastName = lname;

	public String getLastName() {
		return this.lastName;

	public void setAge(int age) {
		this.age = age;

	public int getAge() {
		return this.age;

	public void setId(int id) { = id;

	public int getId() {

	public String toString() {
		return new StringBuffer(" First Name : ").append(this.firstName)
				.append(" Last Name : ").append(this.lastName)
				.append(" Age : ").append(this.age).append(" ID : ")
				.append(" " + this.list).toString();



public class GsonPrettyPrintingExample {

	public static void main(String[] args) {

		Student obj = new Student("Jack", "James", 10, 200);

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

		Gson uglyJson = new Gson();

		String pretJson = prettyGson.toJson(obj);

		System.out.println("Pretty printing: "+pretJson);

		String ugJason = uglyJson.toJson(obj);

		System.out.println("\nUgly printing: "+ugJason);



Pretty printing: {
  "id": 200,
  "firstName": "Jack",
  "lastName": "James",
  "age": 10,
  "list": [

Ugly printing: {"id":200,"firstName":"Jack","lastName":"James","age":10,"list":["Str1","Str2","Str3"]}

This was an example on how to enable pretty print JSON Output using Gson.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books