Convert Java Object to JSON File in Java using Gson

Tags: JSON gson Gson toJson FileWriter HashMap to JSON file ArrayList to JSON file List to JSON file Object to JSON file

In this Java Gson tutorial we learn how to use the Gson library to convert a Java object into JSON file using the Gson.toJson() method with FileWriter. Via different Java program examples we show you how to convert a JSON file from an object of HashMap, ArrayList, an object of a custom defined class or a List of objects.

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

How to convert a HashMap object to JSON file

In the following Java example we learn how to use the Gson.toJson to convert an object of HashMap to JSON file content.

JavaObjectToJsonFileExample1.java

import com.google.gson.Gson;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JavaObjectToJsonFileExample1 {
    public static void main(String... args) throws IOException {
        Map<String, Object> data = new HashMap<>();
        data.put("firstName", "Simple");
        data.put("lastName", "Solution");
        data.put("email", "contact@simplesolution.dev");
        data.put("website", "https://simplesolution.dev");
        Map<String, String> address = new HashMap<>();
        address.put("street", "Simple Street");
        address.put("city", "Sample City");
        address.put("country", "Country Name");
        data.put("address", address);

        Gson gson = new Gson();
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\data.json")) {
            gson.toJson(data, fileWriter);
        }

        System.out.println("Write file successfully at D:\\Data\\data.json");
    }
}
The output is:
Write file successfully at D:\Data\data.json

How to convert a ArrayList object to JSON file

In the following Java example we learn how to use the Gson.toJson to convert an object of ArrayList to JSON file content.

JavaObjectToJsonFileExample2.java

import com.google.gson.Gson;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaObjectToJsonFileExample2 {
    public static void main(String... args) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> customer1 = new HashMap<>();
        customer1.put("firstName", "Simple");
        customer1.put("lastName", "Solution");
        customer1.put("email", "contact@simplesolution.dev");
        customer1.put("website", "https://simplesolution.dev");
        Map<String, String> address1 = new HashMap<>();
        address1.put("street", "Simple Street");
        address1.put("city", "Sample City");
        address1.put("country", "Country Name");
        customer1.put("address", address1);
        list.add(customer1);

        Map<String, Object> customer2 = new HashMap<>();
        customer2.put("firstName", "Java");
        customer2.put("lastName", "Tutorial");
        customer2.put("email", "java@simplesolution.dev");
        customer2.put("website", "https://simplesolution.dev/java");
        Map<String, String> address2 = new HashMap<>();
        address2.put("street", "Simple Street");
        address2.put("city", "Sample City");
        address2.put("country", "Country Name");
        customer2.put("address", address2);
        list.add(customer2);

        Gson gson = new Gson();
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\list.json")) {
            gson.toJson(list, fileWriter);
        }

        System.out.println("Write file successfully at D:\\Data\\list.json");
    }
}
The output is:
Write file successfully at D:\Data\list.json

How to convert a Java object to JSON file

For example we have Address and Customer class definition as below.

Address.java

public class Address {
    private String street;
    private String city;
    private String country;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Customer.java

public class Customer {
    private String firstName;
    private String lastName;
    private String email;
    private String website;
    private Address address;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

In the following Java example we learn how to use the Gson.toJson to convert a Customer object to JSON file content.

JavaObjectToJsonFileExample3.java

import com.google.gson.Gson;

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

public class JavaObjectToJsonFileExample3 {
    public static void main(String... args) throws IOException {
        Customer customer = new Customer();
        customer.setFirstName("Simple");
        customer.setLastName("Solution");
        customer.setEmail("contact@simplesolution.dev");
        customer.setWebsite("https://simplesolution.dev");
        Address address = new Address();
        address.setStreet("Testing Street");
        address.setCity("Sample City");
        address.setCountry("Country Name");
        customer.setAddress(address);

        Gson gson = new Gson();
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\customer.json")) {
            gson.toJson(customer, fileWriter);
        }

        System.out.println("Write file successfully at D:\\Data\\customer.json");
    }
}
The output is:
Write file successfully at D:\Data\customer.json

How to convert a List of objects to JSON file

In the following Java example we learn how to use the Gson.toJson to convert a List of Customer objects to JSON file content.

JavaObjectToJsonFileExample4.java

import com.google.gson.Gson;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JavaObjectToJsonFileExample4 {
    public static void main(String... args) throws IOException {
        List<Customer> listOfCustomers = new ArrayList<>();

        Customer customer1 = new Customer();
        customer1.setFirstName("Simple");
        customer1.setLastName("Solution");
        customer1.setEmail("contact@simplesolution.dev");
        customer1.setWebsite("https://simplesolution.dev");
        Address address1 = new Address();
        address1.setStreet("Testing Street");
        address1.setCity("Sample City");
        address1.setCountry("Country Name");
        customer1.setAddress(address1);
        listOfCustomers.add(customer1);

        Customer customer2 = new Customer();
        customer2.setFirstName("Java");
        customer2.setLastName("Tutorial");
        customer2.setEmail("java@simplesolution.dev");
        customer2.setWebsite("https://simplesolution.dev/java");
        Address address2 = new Address();
        address2.setStreet("Testing Street");
        address2.setCity("Sample City");
        address2.setCountry("Country Name");
        customer2.setAddress(address2);
        listOfCustomers.add(customer2);

        Gson gson = new Gson();
        try(FileWriter fileWriter = new FileWriter("D:\\Data\\customer-list.json")) {
            gson.toJson(listOfCustomers, fileWriter);
        }

        System.out.println("Write file successfully at D:\\Data\\customer-list.json");
    }
}
The output is:
Write file successfully at D:\Data\customer-list.json

Happy Coding 😊