Gson – Write JSON to a File with Pretty Print example shows how to write JSON to a file with pretty printing enabled. By default, Gson prints JSON data in a compact format, but we can easily enable pretty print.
How to save JSON to a file with pretty print?
When we write JSON data to a file, by default, the Gson library writes it in a compact format, that is there are no indentations and spaces. For a large object or list of multiple objects, it becomes very hard to read the file.
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 |
package com.javacodeexamples.gsonexamples; import java.io.FileWriter; import java.io.Writer; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonWriteFilePrettyExample { public static void main(String[] args) { String strFilePath = "D:\\students.json"; List<Student> list = new ArrayList<Student>(); list.add(new Student(1, "Jack", 74)); list.add(new Student(2, "Jill", 57)); list.add(new Student(3, "Sam", 89)); try(Writer writerObject = new FileWriter(strFilePath)){ Gson gsonObject = new Gson(); gsonObject.toJson(list, writerObject); }catch(Exception e) { e.printStackTrace(); } } } class Student{ private int rollNumber; private String name; private int marks; public Student(int rollNumber, String name, int marks) { this.rollNumber = rollNumber; this.name = name; this.marks = marks; } public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } } |
Here is the content of the file students.json.
1 |
[{"rollNumber":1,"name":"Jack","marks":74},{"rollNumber":2,"name":"Jill","marks":57},{"rollNumber":3,"name":"Sam","marks":89}] |
As we can see, the Gson library printed the JSON in a compact format. However, enabling pretty printing is very easy. All we need to do is to use the GsonBuilder to create an object of the Gson class followed by calling setPrettyPrinting
method instead of using the Gson constructor.
Here is how to enable pretty print while saving the JSON to a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
String strFilePath = "D:\\students.json"; List<Student> list = new ArrayList<Student>(); list.add(new Student(1, "Jack", 74)); list.add(new Student(2, "Jill", 57)); list.add(new Student(3, "Sam", 89)); try(Writer writerObject = new FileWriter(strFilePath)){ /* * Create Gson object using the GsonBuilder * and call the setPrettyPrinting method. */ Gson gsonObject = new GsonBuilder() .setPrettyPrinting() .create(); gsonObject.toJson(list, writerObject); }catch(Exception e) { e.printStackTrace(); } |
Here is the content of the file students.json after enabling the pretty print.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[ { "rollNumber": 1, "name": "Jack", "marks": 74 }, { "rollNumber": 2, "name": "Jill", "marks": 57 }, { "rollNumber": 3, "name": "Sam", "marks": 89 } ] |
Please let me know your views in the comments section below.