How to write json data to a file in Java

Published by admin on

Java write json data to a file

How to write JSON data to a file in Java :

JSON or javascript object notation is a simple form to represent complex data. Using JSON, we can easily transfer data to a different platform. For example, we can create one JSON object on our web application and share it with an Android or iOS application. As JSON rules are standard, all applications can share any information easily.
In this tutorial, we will learn how to write JSON data to a file in Java. Our program will create one file and write JSON data to it.

Using GSON to play with JSON :

GSON is one mostly used Java library to deal with JSON data in Java. If you are developing an Android application, you can use it on Android as well. This library is backed by Google and it is really easy to integrate on Gradle or Maven projects.

Github Page link

Gradle :

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

Maven :

  com.google.code.gson
  gson
  2.8.5

You can get the latest version information from the Github page mentioned above.

Java Example :

We will check two different examples to write JSON data to a file: JSON object and JSON array.

1. Writing JSON object to a file :

Let’s try to write a simple Java Object to a file :
Object class (Student.java) :

public class Student {
    private String name;
    private int rank;
    private String schoolName;

    public Student(String name, int rank, String schoolName) {
        this.name = name;
        this.rank = rank;
        this.schoolName = schoolName;
    }

    public Student(String name, int rank) {
        this(name, rank, null);
    }
}

Main class (ExampleJSON.java) :

import com.google.gson.Gson;

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

public class ExampleJSON {
    public static void main(String[] args) {
        Gson gson = new Gson();

        Student s = new Student("Alex", 3, "School A");

        try {
            String jsonString = gson.toJson(s);

            FileWriter writer = new FileWriter("C:\\example.json");
            writer.write(jsonString);
            writer.close();
        } catch (IOException e) {
            System.out.println("exception " + e.getMessage());
            e.printStackTrace();
        }
    }
}

In the above example, we have created one Student object s and convert it to a String first. toJson method is used to convert a Java object to a JSON string.
Then, using a FileWriter, we wrote it to the file example.json.

This file will look like as below :
java write json to a file

As you can see that the result is actually a JSON representation of the Student object.

2. Writing JSON array to a file :

Now, let’s modify the above program and try to write one JSON array to the file. Change the main example file as below :

import com.google.gson.Gson;

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

public class ExampleJSON {
    public static void main(String[] args) {
        Gson gson = new Gson();

        List studentsList = new ArrayList<>();
        studentsList.add(new Student("Alex", 3, "School A"));
        studentsList.add(new Student("Bryan", 5));

        try {
            String jsonString = gson.toJson(studentsList);
            FileWriter writer = new FileWriter("C:\\example.json");
            writer.write(jsonString);
            writer.close();
        } catch (IOException e) {
            System.out.println("exception " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Now, open the file :
java write json to a file
As you can see that the file contains one JSON array with two objects as we have put in the program above.
You can try to implement other complex JSON objects and write them using GSON to a file.

 

You might also like :

Categories: java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *