A Jackson API is a java based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We can process a JSON in three different ways using Streaming API, Tree Model, and Data Binding.
We can Pretty print JSON using the writerWithDefaultPrettyPrinter() of ObjectMapper class, it is a factory method for constructing ObjectWriter that will serialize objects using the default pretty printer for indentation.
Syntax
public ObjectWriter writerWithDefaultPrettyPrinter()
Example
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PrettyPrintJsonJacksonTest {
public static void main(String[] args) throws IOException {
String data = "{\"Age\":30,\"Technologies\": [\"Java\",\"Spark\",\"Python\"],\"Name\":\"Adithya\"}";
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(data, Object.class);
String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); // Pretty print JSON
System.out.println(jsonStr);
}
}Output
{
"Age" : 30,
"Technologies" : [ "Java", "Spark", "Python" ],
"Name" : "Adithya"
}