Writing json data using Jackson JsonGenerator

The below post explains how to write json data to file using JsonGenerator. JsonGenerator writes the data to a file in streaming way, helping the developer using the api to avoid creating an object representation of the data.
We can get an instance of JsonGenerator with the help of an instance of JsonFactory as shown below.

JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator = jsonFactory.createGenerator(fw);

where fw is a FileWriter

The methods of JsonGenerator that are used are as mentioned below
1) writeStartObject sends the character “{” to the stream marking the start of the object.
2) writeEndObject sends the character “}” to the stream marking the end of the object.
3) writeStartArray sends the character “[” to the stream marking the start of the array.
4) writeEndArray sends the character “]” to the stream marking the end of the array.
5) writeObjectField sends the field name and its value together to the stream.
6) writeFieldName sends only the field name to the stream.

Main Code


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 ProcessingDemo5 {
    public static void main(String[] args) throws IOException {
        JsonFactory jsonFactory = new JsonFactory();
        File file = new File("jsondata5.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]}

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