- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pretty print JSON using Jackson library in Java?
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" }
- Related Articles
- Pretty print JSON using org.json library in Java?\n
- Pretty print JSON using the flexjson library in Java?\n
- How to pretty print JSON using the Gson library in Java?
- Pretty print JSON using javax.json API in Java?\n
- Convert JSON to/from Map using Jackson library in Java?
- How to convert Java object to JSON using Jackson library?
- How to pretty print json using javascript?
- Convert CSV to JSON using the Jackson library in Java?\n
- How to convert a JSON to Java Object using the Jackson library in Java?\n
- How to convert a List to JSON array using the Jackson library in Java?
- JSON Schema Support using Jackson in Java?
- How to ignore a field of JSON object using the Jackson library in Java?\n
- Convert POJO to XML using the Jackson library in Java?
- Convert XML to POJO using the Jackson library in Java?
- How to create a JSON using Jackson Tree Model in Java?

Advertisements