Printing json data in a formatted way

This post explains how to print json data in a properly formatted way, whether the destination is console, file etc.

For example when the below code prints the json data to a file, the output will be as shown below

Initial Code


package package11;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

public class PrettyPrintDemo {
    public static void main(String[] args) throws IOException {
        JsonFactory jsonFactory = new JsonFactory();
        File file = new File("jsondata6.json");
        try(FileWriter fw = new FileWriter(file);) {
            JsonGenerator jsonGenerator = jsonFactory.createGenerator(fw);
            jsonGenerator.writeStartObject();
                jsonGenerator.writeObjectField("firstName", "Duke");
                jsonGenerator.writeObjectField("lastName", "Java");
                jsonGenerator.writeObjectField("age", 18);
                jsonGenerator.writeObjectField("streetAddress", "100 Internet Dr");
                jsonGenerator.writeObjectField("city", "JavaTown");
                jsonGenerator.writeObjectField("state", "JA");
                jsonGenerator.writeObjectField("postalCode", "12345");
                jsonGenerator.writeFieldName("phoneNumbers");
                jsonGenerator.writeStartArray();
                    jsonGenerator.writeStartObject();
                        jsonGenerator.writeObjectField("Mobile", "111-111-1111");
                    jsonGenerator.writeEndObject();
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeObjectField("Home", "222-222-2222");
                jsonGenerator.writeEndObject();
                jsonGenerator.writeEndArray();
                jsonGenerator.writeFieldName("numbers");
                jsonGenerator.writeStartArray();
                    jsonGenerator.writeNumber(1);
                    jsonGenerator.writeNumber(2);
                    jsonGenerator.writeNumber(3);
                    jsonGenerator.writeNumber(4);
                jsonGenerator.writeEndArray();
            jsonGenerator.writeEndObject();
            
            jsonGenerator.flush();
            jsonGenerator.close();
        }
    }
}

Output

{"firstName":"Duke","lastName":"Java","age":18,"streetAddress":"100 Internet Dr","city":"JavaTown","state":"JA","postalCode":"12345","phoneNumbers":[{"Mobile":"111-111-1111"},{"Home":"222-222-2222"}],"numbers":[1,2,3,4]}

The output is not easily user readable. So we can format the output by adding one line of below code
jsonGenerator.useDefaultPrettyPrinter();
Just after obtaining the JsonGenerator instance.

Below is the modified code


package package11;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

public class PrettyPrintDemo {
    public static void main(String[] args) throws IOException {
        JsonFactory jsonFactory = new JsonFactory();
        File file = new File("jsondata6.json");
        try(FileWriter fw = new FileWriter(file);) {
            JsonGenerator jsonGenerator = jsonFactory.createGenerator(fw);
            jsonGenerator.useDefaultPrettyPrinter();
            jsonGenerator.writeStartObject();
                jsonGenerator.writeObjectField("firstName", "Duke");
                jsonGenerator.writeObjectField("lastName", "Java");
                jsonGenerator.writeObjectField("age", 18);
                jsonGenerator.writeObjectField("streetAddress", "100 Internet Dr");
                jsonGenerator.writeObjectField("city", "JavaTown");
                jsonGenerator.writeObjectField("state", "JA");
                jsonGenerator.writeObjectField("postalCode", "12345");
                jsonGenerator.writeFieldName("phoneNumbers");
                jsonGenerator.writeStartArray();
                    jsonGenerator.writeStartObject();
                        jsonGenerator.writeObjectField("Mobile", "111-111-1111");
                    jsonGenerator.writeEndObject();
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeObjectField("Home", "222-222-2222");
                jsonGenerator.writeEndObject();
                jsonGenerator.writeEndArray();
                jsonGenerator.writeFieldName("numbers");
                jsonGenerator.writeStartArray();
                    jsonGenerator.writeNumber(1);
                    jsonGenerator.writeNumber(2);
                    jsonGenerator.writeNumber(3);
                    jsonGenerator.writeNumber(4);
                jsonGenerator.writeEndArray();
            jsonGenerator.writeEndObject();
            
            jsonGenerator.flush();
            jsonGenerator.close();
        }
    }
}

The output will be as shown below

Output


{
  "firstName" : "Duke",
  "lastName" : "Java",
  "age" : 18,
  "streetAddress" : "100 Internet Dr",
  "city" : "JavaTown",
  "state" : "JA",
  "postalCode" : "12345",
  "phoneNumbers" : [ {
    "Mobile" : "111-111-1111"
  }, {
    "Home" : "222-222-2222"
  } ],
  "numbers" : [ 1, 2, 3, 4 ]
}

JsonGenerator by default uses an instance of DefaultPrettyPrinter class which adds two spaces at the beginning of each line and use platform specific line feeds.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s