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 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
8 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
mkyongwasifiriskaniPeter McKenna Recent comment authors
newest oldest most voted
wasif
Guest
wasif

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”:”amirzaman34@gamil.com”,
“cont”:”033580000″
},

{
“roll_no”:2,
“name”:”yasir”,
“email”:”yasirkhan45@gmail.com”,
“cont”:”0310643546″
},

{
“roll_no”:”3″,
“name”:”jawad”,
“email”:”jawadyounis30@gamil.com”,
“cont”:”031174397″
},

{
“roll_no”:”4″,
“name”:”shazeb”,
“email”:”shazeb340@gamil.com”,
“cont”:”3013479474″
},
{

“roll_no”:”5″,
“name”:”saad”,
“email”:”saad410@gamil.com”,
“cont”:”033179337″
}
]
}

iris
Guest
iris

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

kani
Guest
kani

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

Peter McKenna
Guest
Peter McKenna

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

trackback
Como converter objetos Java para JSON e vice versa (Jackson) | Casa Do Desenvolvedor

[…] Nota A saída JSON acima é difícil de ler. Você pode melhor isso adicionando a pretty print feature. […]

Chris Fontenot
Guest
Chris Fontenot

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

John
Guest
John

That work for me too.

nag
Guest
nag

Hi,

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

Thanks

Gabriel
Guest
Gabriel

Use this instead:

mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);

Regards.

chandra sekhar Reddy
Guest
chandra sekhar Reddy

gabrel:

this one corrcet thanks

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

[…] Output {"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"} Note Above JSON output is hard to read. You can enhance it by enable the pretty print feature. […]