Main Tutorials

Jackson – How to enable pretty print JSON output

In Jackson, we can use writerWithDefaultPrettyPrinter() to pretty print the JSON output.

Tested with Jackson 2.9.8

1. Pretty Print JSON

1.1 By default, Jackson print in compact format:


	ObjectMapper mapper = new ObjectMapper();
	Staff staff = createStaff();
	String json = mapper.writeValueAsString(staff);
	System.out.println(json);

Output


{"name":"mkyong","age":38,"skills":["java","python","node","kotlin"]}

1.2 To enable pretty print on demand.


	ObjectMapper mapper = new ObjectMapper();
	Staff staff = createStaff();
	// pretty print
	String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff);
	System.out.println(json);

Output


{
  "name" : "mkyong",
  "age" : 38,
  "skills" : [ "java", "python", "node", "kotlin" ]
}

1.3 To enable pretty print globally.


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

	// pretty print
	ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
	Staff staff = createStaff();
	String json = mapper.writeValueAsString(staff);
	System.out.println(json);

Output


{
  "name" : "mkyong",
  "age" : 38,
  "skills" : [ "java", "python", "node", "kotlin" ]
}
Note
To display the pretty print JSON output on an HTML page, wraps it with pre tags.
<pre>${pretty-print-json-output}</pre>
Note – 12/12/2013
The article is updated to use writerWithDefaultPrettyPrinter(), the old defaultPrettyPrintingWriter() is deprecated.

References

About Author

author image
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

Subscribe
Notify of
15 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
nese ceylan
3 years ago

thank you so muchh

Jennifer Puerta
3 years ago

Brilliant!

Chris Fontenot
10 years ago

the Jackson source code suggests using writerWithDefaultPrettyPrinter() instead. works for me.

John
10 years ago
Reply to  Chris Fontenot

That work for me too.

Aastha Sethai
4 years ago

Hi, I wanted to know if it is possible to {“name”:”mkyong”,”age”:38,”skills”:[“java”,”python”,”node”,”kotlin”]}
change this to {“Fullname”:”mkyong”,”age”:38,”skills”:[“java”,”python”,”node”,”kotlin”]}; while converting without creating a copy of the java object.

iris
5 years ago

Nice article, sometime I use https://jsonformatter.org/json-pretty-print tool to do the same.

kani
8 years ago

how to convert a json string value {“name”:”1″} to json slash quotes format value {“name”:”1″} in java

nag
10 years ago

Hi,

defaultPrettyPrintingWriter() seems to be deprecated.
Can you suggest me any other way?

Thanks

Gabriel
10 years ago
Reply to  nag

Use this instead:

mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);

Regards.

chandra sekhar Reddy
10 years ago
Reply to  Gabriel

gabrel:

this one corrcet thanks

wasif
5 years ago

mkyong did you tell me that how i can parse json array using loop..if my array is something like this {“student”:[
{
“roll_no”:”1″,
“name”:”amir” ,
“email”:”[email protected]”,
“cont”:”033580000″
},

{
“roll_no”:2,
“name”:”yasir”,
“email”:”[email protected]”,
“cont”:”0310643546″
},

{
“roll_no”:”3″,
“name”:”jawad”,
“email”:”[email protected]”,
“cont”:”031174397″
},

{
“roll_no”:”4″,
“name”:”shazeb”,
“email”:”[email protected]”,
“cont”:”3013479474″
},
{

“roll_no”:”5″,
“name”:”saad”,
“email”:”[email protected]”,
“cont”:”033179337″
}
]
}

Peter McKenna
9 years ago

You can also use an online JSON viewer like http://jsonprettyprint.net. Super simple but it works.