Gson – Write JSON to a file example shows how to write JSON data to a file using the Gson library in Java. This example also shows how to use the toJson method to save JSON to a file.
How to write JSON data to a file?
Gson library provides various ways to save JSON data to a file. The most common method is to use the toJson
method of the Gson class. We can utilize the toJson
method that accepts a writer object along with the JSON that we want to write to a file.
1 |
public void toJson(Object src, Appendable writer) throws JsonIOException |
This toJson
method serializes the given object in JSON representation and writes to the specified writer.
Here is a quick example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.javacodeexamples.gsonexamples; import java.io.FileWriter; import java.io.Writer; import com.google.gson.Gson; public class GsonWriteJSONToFileExample { public static void main(String[] args) { //file path we want to write to String strFilePath = "D:\\employees.json"; //Employee objects we want to save to a file Emp emp1 = new Emp("EMP1", "John", 23); //create a write object for the file try(Writer writerObject = new FileWriter(strFilePath)){ Gson gsonObject = new Gson(); /* * Use the toJson method and specify * the writer and the object we want to write */ gsonObject.toJson(emp1, writerObject); }catch(Exception e) { e.printStackTrace(); } } } class Emp{ private String id; private String name; private int age; public Emp(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Here is the file content after running the program.
1 |
{"id":"EMP1","name":"John","age":23} |
Instead of writing individual employee objects, we can also write a list of employees as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//file path we want to write to String strFilePath = "D:\\employees.json"; //List of Employee objects we want to save to a file List<Emp> list = new ArrayList<Emp>(); list.add(new Emp("EMP1", "John", 23)); list.add(new Emp("EMP2", "Alex", 45)); list.add(new Emp("EMP3", "June", 32)); //create a write object for the file try(Writer writerObject = new FileWriter(strFilePath)){ Gson gsonObject = new Gson(); /* * Use the toJson method and specify * the writer and the list we want to write */ gsonObject.toJson(list, writerObject); }catch(Exception e) { e.printStackTrace(); } |
In this case, the Gson automatically converts a List object to a JSON array and writes all employee objects as its elements. Here is the file content after running the program.
1 |
[{"id":"EMP1","name":"John","age":23},{"id":"EMP2","name":"Alex","age":45},{"id":"EMP3","name":"June","age":32}] |
Important Note: I have used try with resources to open a writer that closes the writer automatically. But if you are not using that and not seeing the JSON output in the file, make sure to flush and close the writer object.
Please let me know your views in the comments section below.