Java Gson Read JSON File using JsonReader

Tags: JSON gson Gson JsonReader JsonReader beginObject JsonReader endObject JsonReader nextName JsonReader nextString FileReader

In this Java Gson tutorial we learn how to use the class of Gson library to read JSON file content.

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: '', 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.


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

What is the JsonReader class?

The is a class of Gson library to allow reading JSON content as a stream of tokens.

How to read JSON file using JsonReader

For example, we have a JSON File as below



In the following Java program we show you how to read the above JSON file using the JsonReader class.



public class JsonReaderExample {
    public static void main(String... args) {
        try(FileReader fileReader = new FileReader("D:\\Data\\test.json");
            JsonReader jsonReader = new JsonReader(fileReader)) {
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                String value = jsonReader.nextString();

                System.out.println("Name: " + name);
                System.out.println("Value: " + value + "\n");

        } catch (FileNotFoundException e) {
        } catch (IOException e) {

The output is:
Name: firstName
Value: Simple

Name: lastName
Value: Solution

Name: email

Name: website

Happy Coding 😊