Home » Core Java » gson » GsonBuilder » Enable Pretty Print JSON Output using Gson example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. 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.

 
 
 
 
 
Student.java:

package com.javacodegeeks.java.core;

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;
		this.id = id;

		this.list = new ArrayList<String>();
		this.list.add("Str1");
		this.list.add("Str2");
		this.list.add("Str3");

	}

	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) {
		this.id = id;
	}

	public int getId() {
		return this.id;
	}

	@Override
	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.id).append(" " + this.list).toString();
	}
}

GsonPrettyPrintingExample.java:

package com.javacodegeeks.java.core;

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

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);

	}
}

output:

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

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close