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
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
The article is updated to use
writerWithDefaultPrettyPrinter()
, the old defaultPrettyPrintingWriter()
is deprecated.
thank you so muchh
Brilliant!
the Jackson source code suggests using writerWithDefaultPrettyPrinter() instead. works for me.
The article is updated, thanks.
That work for me too.
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.
Nice article, sometime I use https://jsonformatter.org/json-pretty-print tool to do the same.
how to convert a json string value {“name”:”1″} to json slash quotes format value {“name”:”1″} in java
Hi,
defaultPrettyPrintingWriter() seems to be deprecated.
Can you suggest me any other way?
Thanks
The article is updated to use
writerWithDefaultPrettyPrinter()
, the olddefaultPrettyPrintingWriter()
is deprecated.Use this instead:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
Regards.
gabrel:
this one corrcet thanks
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″
}
]
}
Please refer to below example :
List<Person> ppl2 = Arrays.asList(mapper.readValue(json, Person[].class));
https://www.mkyong.com/java/jackson-convert-json-array-string-to-list/
You can also use an online JSON viewer like http://jsonprettyprint.net. Super simple but it works.