Print object as JSON

Often we need to log or print objects and variables to the console/logs for debugging. Here is how to print an object as JSON in Java.

GSON. Maven Dependency

To print object as JSON we’re going to use Gson - a very powerful library for JSON serialization/deserialization (in the current context converting from object to JSON and from JSON to object).

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.6.2</version>
</dependency>

Java object example

public class Book {
    public String title;
    public String author;
    public int numberOfPages;
    public List<String> chapterTitles;

    public Book(String title, String author, int numberOfPages, List<String> chapterTitles) {
    	this.title = title;
    	this.author = author;
    	this.numberOfPages = numberOfPages;
    	this.chapterTitles = chapterTitles;
    }
}

If we try to print this Java object

List<String> chapters = 
      Arrays.asList(new String[]{"The way Java works", "Code structure in Java", 
                                  "Making your first Java objects", "Java kewords"});
Book book = new Book("Head First Java", "Kathy Sierra, Bert Bates", 688, chapters);
System.out.println(book);

We get the output like this

$Book@58cd06cb

That’s not very informative, is it?

We need all object’s fields printed.

The JSON format and the Gson library let us print the Java object as JSON easily.

System.out.println(new Gson().toJson(book));

This is how a Java object will lookin JSON format

{"title": "Head First Java","author": "Kathy Sierra, Bert Bates","numberOfPages": 688,"chapterTitles": ["The way Java works","Code structure in Java","Making your first Java objects","Java keywords"]}

Override toString() to print object as JSON

Let’s add (override) the method toString to our Book class to print any Book Java object as JSON.

public class Book {
    public String title;
    public String author;
    public int numberOfPages;
    public List<String> chapterTitles;

    public Book(String title, String author, int numberOfPages, List<String> chapterTitles) {
        this.title = title;
        this.author = author;
        this.numberOfPages = numberOfPages;
        this.chapterTitles = chapterTitles;
    }

    public String toString() {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(this);
    }
}

Now we’re going to print our Java object as JSON

List<String> chapters = 
        Arrays.asList(new String[]{"The way Java works", "Code structure in Java", 
                                    "Making your first Java objects", "Java keywords"});
Book book = new Book("Head First Java", "Kathy Sierra, Bert Bates", 688, chapters);
System.out.println(book);

Readable console output in JSON format

{
  "title": "Head First Java",
  "author": "Kathy Sierra, Bert Bates",
  "numberOfPages": 688,
  "chapterTitles": [
    "The way Java works",
    "Code structure in Java",
    "Making your first Java objects",
    "Java keywords"
  ]
}

Java objects in JSON format (JSON objects) now look much better. Feel the difference.

In general you can print any Java object as JSON.

public void printObject(Object object) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    System.out.println(gson.toJson(object));
}

// Usage 
List<String> chapters = 
        Arrays.asList(new String[]{"The way Java works", "Code structure in Java", 
                                    "Making your first Java objects", "Java keywords"});
Book book = new Book("Head First Java", "Kathy Sierra, Bert Bates", 688, chapters);
printObject(book);

Note: It prints all object’s fields even if they are also objects


You may also find these posts interesting: