Pretty print JSON using the flexjson library in Java?

JavaJSONObject Oriented ProgrammingProgramming

The Flexjson is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can pretty-print JSON using the prettyPrint(boolean prettyPrint) method of JSONSerializer class.


public JSONSerializer prettyPrint(boolean prettyPrint)

In the below program, Pretty print JSON using flexjson library


import flexjson.*;
public class PrettyPrintJSONTest {
   public static void main(String[] args) {
      JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print
      Employee emp = new Employee("Vamsi", "105", "Python Developer", "Python", "Pune");
      String jsonStr = serializer.serialize(emp);
// Employee class
class Employee {
   private String name, id, designation, technology, location;
   public Employee(String name, String id, String designation, String technology, String location) {
      super(); = name; = id;
      this.designation = designation; = technology;
      this.location = location;
   public String getName() {
      return name;
   public String getId() {
      return id;
   public String getDesignation() {
      return designation;
   public String getTechnology() {
      return technology;
   public String getLocation() {
      return location;


 "class": "Employee",
 "designation": "Python Developer",
 "id": "105",
 "location": "Pune",
 "name": "Vamsi",
 "technology": "Python"
Updated on 06-Jul-2020 05:53:33