Jackson

Enable Pretty Print JSON Output using Jackson example

As you might have noticed in the previous JSON tutorials, the output of the programs is not properly aligned, which makes it hard to read.

For this demonstration we are going to use the Student class from this example.

 
 
 
 
 
 

Student.java:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.javacodegeeks.java.core;
 
public class Student {
 
    private int id;
    private String firstName;
    private String lastName;
    private int age;
 
    public Student(){
 
    }
 
    public Student(String fname, String lname, int age, int id){
        this.firstName = fname;
        this.lastName  = lname;
        this.age        = age;
        this.id         = id;
    }
 
    public void setFirstName(String fname) {
        this.firstName = fname;
    }
 
    public String getFirstName() {
        return this.firstName;
    }
 
    public void setLastName(String lname) {
        this.lastName = lname;
    }
 
    public String getLastName() {
        return this.lastName;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public int getAge() {
        return this.age;
    }
 
    public void setId(int id){
        this.id = id;
    }
 
    public int getId(){
        return this.id;
    }
 
    @Override
    public String toString() {
        return new StringBuffer(" First Name : ").append(this.firstName)
                .append(" Last Name : ").append(this.lastName).append(" Age : ").append(this.age).append(" ID : ").append(this.id).toString();
    }
 
}

JacksonPrettyPrintingExample.java:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.javacodegeeks.java.core;
 
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonPrettyPrintingExample {
 
  public static void main(String[] args) {
 
    Student student = new Student("Jack", "Jones",12, 100 );
 
    ObjectMapper mapper = new ObjectMapper();
 
    try {
 
        System.out.println("Default output:"+mapper.writeValueAsString(student));
 
        System.out.println("Pretty printing:\n"+mapper.defaultPrettyPrintingWriter().writeValueAsString(student));     
 
    } catch (JsonGenerationException e) {
 
        e.printStackTrace();
 
    } catch (JsonMappingException e) {
 
        e.printStackTrace();
 
    } catch (IOException e) {
 
        e.printStackTrace();
 
    }
 
    }
 
}

output:

Default output:{"id":100,"firstName":"Jack","lastName":"Jones","age":12}

Pretty printing:
{
  "id" : 100,
  "firstName" : "Jack",
  "lastName" : "Jones",
  "age" : 12
}

 
This was an example on how to enable Pretty Print JSON Output using Jackson.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button