Java Convert classpath resource JSON File to Java Object using Gson
Tags: JSON gson Gson fromJson Gson TypeToken classpath JSON file to HashMap classpath JSON file to List classpath JSON file to object IOUtils InputStream ClassLoader ClassLoader getResourceAsStream classpath resource
In this Java Gson tutorial, we show you how to read a JSON file from classpath resources and convert the JSON content to Java object using the Gson library and Apache Commons IO library. By different Java example programs we learn how to convert JSON content to a HashMap, a List of Map, an object of a custom defined class or a List of objects.
How to add Gson and Apache Commons IO to the Java project
To use the Gson library and Apache Commons IO library in the Gradle build project, add the following dependencies into the build.gradle file.
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
implementation group: 'commons-io', name: 'commons-io', version: '2.10.0'
To use the Gson library and Apache Commons IO library in the Maven build project, add the following dependencies into the pom.xml file.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.10.0</version>
</dependency>
You can download the Gson jar file from Maven Central at gson-2.8.7.jar
You can download the Apache Commons IO jar file from Maven Central at commons-io-2.10.0.jar
To have more information about the Gson library you can visit the project repository at github.com/google/gson
To have more information about the Apache Commons IO library you can visit the project home page at commons.apache.org/proper/commons-io/
How to convert classpath resource JSON file to HashMap
For example we have a JSON file located in the project source code at src/main/resources/sample1.json with content as below.
src/main/resources/sample1.json
{
"firstName": "Simple",
"lastName": "Solution",
"email": "contact@simplesolution.dev",
"website": "https://simplesolution.dev",
"address": {
"street": "Simple Street",
"city": "City Name",
"country": "Country Name"
}
}
In the following Java example we show you how to read the JSON file above into a JSON String using IOUtils.toString() method. And then convert it to a HashMap object using Gson.fromJson() method.
ResourceJsonFileToJavaObjectExample1.java
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
public class ResourceJsonFileToJavaObjectExample1 {
public static void main(String... args) throws IOException {
Gson gson = new Gson();
ClassLoader classLoader = gson.getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("sample1.json");
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
HashMap<String, String> result = gson.fromJson(jsonString, HashMap.class);
System.out.println("Converted HashMap object: ");
System.out.println(result);
}
}
Converted HashMap object:
{firstName=Simple, lastName=Solution, website=https://simplesolution.dev, address={street=Simple Street, city=City Name, country=Country Name}, email=contact@simplesolution.dev}
How to convert classpath resource JSON file to List of Map
For example we have a JSON file located in the project source code at src/main/resources/sample2.json with content as below.
src/main/resources/sample2.json
[
{
"firstName": "Simple",
"lastName": "Solution",
"email": "contact@simplesolution.dev",
"website": "https://simplesolution.dev",
"address": {
"street": "Simple Street",
"city": "City Name",
"country": "Country Name"
}
},
{
"firstName": "Java",
"lastName": "Tutorial",
"email": "java@simplesolution.dev",
"website": "https://simplesolution.dev",
"address": {
"street": "Test Street",
"city": "City Name",
"country": "Country Name"
}
},
{
"firstName": "Gson",
"lastName": "Learn",
"email": "gson@simplesolution.dev",
"website": "https://simplesolution.dev/tag/gson",
"address": {
"street": "Gson Street",
"city": "City Name",
"country": "Country Name"
}
}
]
In the following Java example we show you how to read the JSON file above into a JSON String using IOUtils.toString() method. And then convert it to a List of Map objects using Gson.fromJson() method.
ResourceJsonFileToJavaObjectExample2.java
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class ResourceJsonFileToJavaObjectExample2 {
public static void main(String... args) throws IOException {
Gson gson = new Gson();
ClassLoader classLoader = gson.getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("sample2.json");
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
List<Map> list = gson.fromJson(jsonString, List.class);
System.out.println("Converted List of Map: ");
for (Map item : list) {
System.out.println(item);
}
}
}
Converted List of Map:
{firstName=Simple, lastName=Solution, email=contact@simplesolution.dev, website=https://simplesolution.dev, address={street=Simple Street, city=City Name, country=Country Name}}
{firstName=Java, lastName=Tutorial, email=java@simplesolution.dev, website=https://simplesolution.dev, address={street=Test Street, city=City Name, country=Country Name}}
{firstName=Gson, lastName=Learn, email=gson@simplesolution.dev, website=https://simplesolution.dev/tag/gson, address={street=Gson Street, city=City Name, country=Country Name}}
How to convert classpath resource JSON file to Java object
In the following Java example program, we show you how to convert the classpath resource JSON file to an object of a custom defined class.
Firstly we define the Address and Customer class to represent the customer record as the below Java code.
Address.java
public class Address {
private String street;
private String city;
private String country;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Customer.java
public class Customer {
private String firstName;
private String lastName;
private String email;
private String website;
private Address address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
The Java program below converts the classpath resource JSON file at src/main/resources/sample1.json to a Customer object.
ResourceJsonFileToJavaObjectExample3.java
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ResourceJsonFileToJavaObjectExample3 {
public static void main(String... args) throws IOException {
Gson gson = new Gson();
ClassLoader classLoader = gson.getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("sample1.json");
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
Customer customer = gson.fromJson(jsonString, Customer.class);
System.out.println("First Name: " + customer.getFirstName());
System.out.println("Last Name: " + customer.getLastName());
System.out.println("Email: " + customer.getEmail());
System.out.println("Website: " + customer.getWebsite());
System.out.println("Street: " + customer.getAddress().getStreet());
System.out.println("City: " + customer.getAddress().getCity());
System.out.println("Country: " + customer.getAddress().getCountry());
}
}
First Name: Simple
Last Name: Solution
Email: contact@simplesolution.dev
Website: https://simplesolution.dev
Street: Simple Street
City: City Name
Country: Country Name
How to convert classpath resource JSON file to Java List of objects
The Java program below converts the classpath resource JSON file at src/main/resources/sample2.json to a List of Customer objects.
ResourceJsonFileToJavaObjectExample4.java
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class ResourceJsonFileToJavaObjectExample4 {
public static void main(String... args) throws IOException {
Gson gson = new Gson();
ClassLoader classLoader = gson.getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("sample2.json");
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
Type type = new TypeToken<List<Customer>>(){}.getType();
List<Customer> customers = gson.fromJson(jsonString, type);
for(Customer customer : customers) {
System.out.println("First Name: " + customer.getFirstName());
System.out.println("Last Name: " + customer.getLastName());
System.out.println("Email: " + customer.getEmail());
System.out.println("Website: " + customer.getWebsite());
System.out.println("Street: " + customer.getAddress().getStreet());
System.out.println("City: " + customer.getAddress().getCity());
System.out.println("Country: " + customer.getAddress().getCountry());
System.out.println();
}
}
}
First Name: Simple
Last Name: Solution
Email: contact@simplesolution.dev
Website: https://simplesolution.dev
Street: Simple Street
City: City Name
Country: Country Name
First Name: Java
Last Name: Tutorial
Email: java@simplesolution.dev
Website: https://simplesolution.dev
Street: Test Street
City: City Name
Country: Country Name
First Name: Gson
Last Name: Learn
Email: gson@simplesolution.dev
Website: https://simplesolution.dev/tag/gson
Street: Gson Street
City: City Name
Country: Country Name
Happy Coding 😊