How to enable pretty print JSON with Jackson
In Jackson, we can use writerWithDefaultPrettyPrinter()
to pretty print the JSON strings.
Table of contents:
- 1. Setup Jackson
- 2. Compact print JSON (Default)
- 3. Pretty print JSON with Jackson
- 4. Pretty print JSON with Jackson (Globally)
- 5. Download Source Code
- 6. References
P.S Tested with Jackson 2.17.0
1. Setup Jackson
Puts jackson-databind
at pom.xml
.
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
2. Compact print JSON (Default)
By default, Jackson prints JSON strings in compact format:
PrettyPrintJsonExample.java
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
public class PrettyPrintJsonExample {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Person person = new Person("mkyong", 42);
String json = mapper.writeValueAsString(person);
System.out.println(json);
}
}
Output
{"name":"mkyong","age":42}
3. Pretty print JSON with Jackson
We can use .writerWithDefaultPrettyPrinter()
to manually enable the pretty print JSON strings.
ObjectMapper mapper = new ObjectMapper();
// enable pretty print
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(...)
PrettyPrintJsonExample.java
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
public class PrettyPrintJsonExample {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Person person = new Person("mkyong", 42);
String json = mapper
.writerWithDefaultPrettyPrinter() // enable pretty print
.writeValueAsString(person);
System.out.println(json);
}
}
Output
{
"name" : "mkyong",
"age" : 42
}
4. Pretty print JSON with Jackson (Globally)
We can enable the SerializationFeature.INDENT_OUTPUT
feature in the ObjectMapper
. This will enable the pretty print JSON globally.
PrettyPrintJsonExample.java
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.mkyong.json.model.Person;
public class PrettyPrintJsonExample {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// enable the pretty print globally
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Person person = new Person("mkyong", 42);
String json = mapper.writeValueAsString(person); // compact-print
System.out.println(json);
}
}
Output
{
"name" : "mkyong",
"age" : 42
}
Note – 12/12/2013
The article is updated to use the new Jackson’s method writerWithDefaultPrettyPrinter()
to enable the pretty print JSON string; the old method defaultPrettyPrintingWriter()
is deprecated.
5. Download Source Code
$ git clone https://github.com/mkyong/java-json
$ cd jackson
6. References
About Author
Comments
Subscribe
1 Comment
Most Voted