Java Gson Write JSON File using JsonWriter

Tags: JSON gson Gson JsonWriter JsonWriter beginObject JsonWriter endObject JsonWriter beginArray JsonWriter endArray FileWriter

In this Java Gson tutorial we learn how to use the com.google.gson.stream.JsonWriter class in the Gson library to write JSON content to file. Via different Java example programs we show you how to write a simple JSON object to file, a multi level JSON content or an array of objects to JSON file.

How to add Gson to the Java project

To use the Gson library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'

To use the Gson library in the Maven build project, add the following dependency into the pom.xml file.

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

Or you can download the Gson jar file from Maven Central at gson-2.8.7.jar

To have more information about the Gson library you can visit the project repository at github.com/google/gson

What is the JsonWriter class?

The com.google.gson.stream.JsonWriter is the class in the Gson library to allow writing JSON values to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans and nulls) as well as the begin and end delimiters of objects and arrays.

How to write a JSON object to file using JsonWriter

JsonWriterExample1.java

import com.google.gson.stream.JsonWriter;

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

public class JsonWriterExample1 {
    public static void main(String... args) {
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\test1.json");
            JsonWriter jsonWriter = new JsonWriter(fileWriter)
        ) {
            jsonWriter.beginObject();
            jsonWriter.name("firstName").value("Simple");
            jsonWriter.name("lastName").value("Solution");
            jsonWriter.name("email").value("contact@simplesolution.dev");
            jsonWriter.name("website").value("https://simplesolution.dev");
            jsonWriter.name("phone").nullValue();
            jsonWriter.endObject();

            System.out.println("Write JSON file successfully at D:\\Data\\test1.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Write JSON file successfully at D:\Data\test1.json

The output file content, D:\Data\test1.json

{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","website":"https://simplesolution.dev","phone":null}

How to write a multi level JSON object to file using JsonWriter

JsonWriterExample2.java

import com.google.gson.stream.JsonWriter;

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

public class JsonWriterExample2 {
    public static void main(String... args) {
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\test2.json");
            JsonWriter jsonWriter = new JsonWriter(fileWriter)
        ) {
            jsonWriter.beginObject();
            jsonWriter.name("firstName").value("Simple");
            jsonWriter.name("lastName").value("Solution");
            jsonWriter.name("email").value("contact@simplesolution.dev");
            jsonWriter.name("website").value("https://simplesolution.dev");

            jsonWriter.name("address");
            jsonWriter.beginObject();
            jsonWriter.name("street").value("Test Street");
            jsonWriter.name("city").value("Sample City");
            jsonWriter.endObject();

            jsonWriter.endObject();

            System.out.println("Write JSON file successfully at D:\\Data\\test2.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Write JSON file successfully at D:\Data\test2.json

The output file content, D:\Data\test2.json

{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","website":"https://simplesolution.dev","address":{"street":"Test Street","city":"Sample City"}}

How to write a JSON array to file using JsonWriter

JsonWriterExample3.java

import com.google.gson.stream.JsonWriter;

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

public class JsonWriterExample3 {
    public static void main(String... args) {
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\test3.json");
            JsonWriter jsonWriter = new JsonWriter(fileWriter)
        ) {
            jsonWriter.beginArray();

            jsonWriter.beginObject();
            jsonWriter.name("firstName").value("Simple");
            jsonWriter.name("lastName").value("Solution");
            jsonWriter.name("email").value("contact@simplesolution.dev");
            jsonWriter.name("website").value("https://simplesolution.dev");

            jsonWriter.name("address");
            jsonWriter.beginObject();
            jsonWriter.name("street").value("Test Street");
            jsonWriter.name("city").value("Sample City");
            jsonWriter.endObject();
            jsonWriter.endObject();

            jsonWriter.beginObject();
            jsonWriter.name("firstName").value("Java");
            jsonWriter.name("lastName").value("Tutorial");
            jsonWriter.name("email").value("java@simplesolution.dev");
            jsonWriter.name("website").value("https://simplesolution.dev/java");

            jsonWriter.name("address");
            jsonWriter.beginObject();
            jsonWriter.name("street").value("123 Street");
            jsonWriter.name("city").value("Sample City");
            jsonWriter.endObject();
            jsonWriter.endObject();

            jsonWriter.endArray();

            System.out.println("Write JSON file successfully at D:\\Data\\test3.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Write JSON file successfully at D:\Data\test3.json

The output file content, D:\Data\test3.json

[{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","website":"https://simplesolution.dev","address":{"street":"Test Street","city":"Sample City"}},{"firstName":"Java","lastName":"Tutorial","email":"java@simplesolution.dev","website":"https://simplesolution.dev/java","address":{"street":"123 Street","city":"Sample City"}}]

Happy Coding 😊