How to enable pretty print JSON output (Jackson)

In this tutorial, we will show you how to use Jackson library to pretty print JSON Object and String in the console and JSP page.

1. Pretty Print JSON Object

Example to convert Object and print its output in JSON format.

		
 User user = new User();
 //...set user data
	
 ObjectMapper mapper = new ObjectMapper();
 System.out.println(mapper.writeValueAsString(user));

But the json output is in compact mode.


{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}

To enable pretty print, use writerWithDefaultPrettyPrinter.


  ObjectMapper mapper = new ObjectMapper();
  System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));

Output


{
  "age" : 29,
  "messages" : [ "msg 1", "msg 2", "msg 3" ],
  "name" : "mkyong"
}

2. Pretty Print JSON String

This is a bit tricky, try to use writerWithDefaultPrettyPrinter again.


  String test = "{\"age\":29,\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"],\"name\":\"mkyong\"}";
  System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(test));

Output, not what we want, json is still in compact mode.


"{\"age\":29,\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"],\"name\":\"mkyong\"}"

To solve it, bind the JSON string to Object before pretty print it.


  String test = "{\"age\":29,\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"],\"name\":\"mkyong\"}";
  Object json = mapper.readValue(test, Object.class);	
  System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));

Output


{
  "age" : 29,
  "messages" : [ "msg 1", "msg 2", "msg 3" ],
  "name" : "mkyong"
}

3. Pretty Print JSON in JSP page

To pretty print JSON data in a JSP page or any other HTML page, just wrap the json data with <pre> tag. This example is using Spring MVC and JSP page.

Controller Class

@Controller
@RequestMapping("/anything")
public class AdminController {

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView index() {

	String test = "{\"age\":29,\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"],\"name\":\"mkyong\"}";
	Object json = mapper.readValue(test, Object.class);	
	
	ModelAndView modelandView = new ModelAndView("viewname");

	modelandView.addObject("output", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
	
	return modelandViewl;

  }

}

In html page.
<pre>${output}</pre>

Note – 12/12/2013
Article is updated to use writerWithDefaultPrettyPrinter(), the old defaultPrettyPrintingWriter() is deprecated.

References

  1. Jackson – High-performance JSON processor.
  2. ObjectMapper 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

avatar
8 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
wasifiriskaniPeter McKennaComo converter objetos Java para JSON e vice versa (Jackson) | Casa Do Desenvolvedor 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. […]