
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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" }
Advertisements