Hello there and welcome to my new article on Java Programming. Today I will show you how to read data from a JSON file by using Gson library. Don’t think too much, it is very simple.

Going forward I will be using a Maven project and I would recommend you to download demo project source code from my Github.

About Gson…

Gson is a Java serialization/deserialization library which is used to covert Java objects to JSON representation or JSON representation to Java objects.

Adding the Dependency…

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

Create the Resource File…

Say, I have a small shop and I am selling fruits. I would like to store information like name, count, etc. for each kind of fruits and maintain a list. I am going to name the file as fruits with extension .json and store it in my resources folder.

[
{
"name": "Apple",
"available": true,
"count": 20
},
{
"name": "Orange",
"available": false,
"count": 0
}
]
view raw fruits.json hosted with ❤ by GitHub

Loading the Data to Java Application…

Here we have to use the path of the JSON file we created earlier and using Gson we can load the data into a list of maps. Where each map will hold the stock information the list will have all the stock iformation.

package org.sydlabz.demo;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class LoadData {
public static List<LinkedHashMap<?, ?>> load() throws IOException {
final String RESOURCE_PATH = "Absolute path of fruits.json";
Gson gsonHandle = new Gson();
Reader jsonReader = Files.newBufferedReader(Paths.get(RESOURCE_PATH));
return new LinkedList<>(gsonHandle.fromJson(jsonReader, List.class));
}
}
view raw LoadData.java hosted with ❤ by GitHub

Traversing the List…

If we take a closer look at the above lines of code, I have used generics and didn’t mentioned what should be the data type on RHS of the map containing stock information. Lets see what is the type of values in RHS of map containing stock information. In the JSON file, we can see that the RHS have three data types – String, Boolean and Real number.

So lets take a look if those data types can be identified from our Java application.

package org.sydlabz.demo;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Console {
public static void showData(List<LinkedHashMap<?, ?>> data) {
for (Map<?, ?> record : data) {
System.out.println(record);
for (Object key : record.keySet()) {
System.out.println("\t" + key.getClass().getName() + " – " + record.get(key).getClass().getName());
}
}
}
}
view raw Console.java hosted with ❤ by GitHub

And take a look at the output generated.

{name=Ford, available=true, count=2.0}
     java.lang.String - java.lang.String
     java.lang.String - java.lang.Boolean
     java.lang.String - java.lang.Double
 {name=Ferrari, available=false, count=0.0}
     java.lang.String - java.lang.String
     java.lang.String - java.lang.Boolean
     java.lang.String - java.lang.Double
 Process finished with exit code 0

As we can see from the generated output, the types of RHS value of each items in the list has been identified which is respectively String, Boolean and Double 🙂

Now we have loaded the data we can process it as we need.

Looking for the Source Code ?

/seyedsahil/gson-file-demo/

Thank you for reading

Seyed Sahil