Convert Java Object to JSON String in Java using Gson
Tags: JSON gson Gson toJson Convert HashMap to JSON Object to JSON List to JSON
In this Java Gson tutorial we learn how to use the Gson library to convert a Java object to a JSON String using the Gson.toJson() method. Via step by step Java example we show you how to convert to a JSON String from a HashMap, a List of HashMap, a Java object or List of Java objects.
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: 'com.google.code.gson', 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.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
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 github.com/google/gson
Convert a HashMap object to JSON String
In the following Java example program we show you how to use the Gson.toJson() method to convert a HashMap object into JSON String object.
JavaObjectToJsonStringExample1.java
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class JavaObjectToJsonStringExample1 {
public static void main(String... args) {
Map<String, String> data = new HashMap<>();
data.put("firstName", "Simple");
data.put("lastName", "Solution");
data.put("email", "contact@simplesolution.dev");
data.put("website", "https://simplesolution.dev");
Gson gson = new Gson();
String jsonString = gson.toJson(data);
System.out.println("Output JSON String:");
System.out.println(jsonString);
}
}
Output JSON String:
{"firstName":"Simple","lastName":"Solution","website":"https://simplesolution.dev","email":"contact@simplesolution.dev"}
Convert a HashMap object to multi level JSON String
We also can convert HashMap to multi level JSON String format as the following Java program.
JavaObjectToJsonStringExample2.java
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class JavaObjectToJsonStringExample2 {
public static void main(String... args) {
Map<String, Object> data = new HashMap<>();
data.put("firstName", "Simple");
data.put("lastName", "Solution");
data.put("email", "contact@simplesolution.dev");
data.put("website", "https://simplesolution.dev");
Map<String, String> address = new HashMap<>();
address.put("street", "Simple Street");
address.put("city", "Sample City");
address.put("country", "Country Name");
data.put("address", address);
Gson gson = new Gson();
String jsonString = gson.toJson(data);
System.out.println("Output JSON String:");
System.out.println(jsonString);
}
}
Output JSON String:
{"firstName":"Simple","lastName":"Solution","website":"https://simplesolution.dev","address":{"country":"Country Name","city":"Sample City","street":"Simple Street"},"email":"contact@simplesolution.dev"}
Convert a list of HashMap to JSON String
In the below example, we show you how to convert a List of HasMap to JSON String.
JavaObjectToJsonStringExample3.java
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaObjectToJsonStringExample3 {
public static void main(String... args) {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> customer1 = new HashMap<>();
customer1.put("firstName", "Simple");
customer1.put("lastName", "Solution");
customer1.put("email", "contact@simplesolution.dev");
customer1.put("website", "https://simplesolution.dev");
Map<String, String> address1 = new HashMap<>();
address1.put("street", "Simple Street");
address1.put("city", "Sample City");
address1.put("country", "Country Name");
customer1.put("address", address1);
list.add(customer1);
Map<String, Object> customer2 = new HashMap<>();
customer2.put("firstName", "Java");
customer2.put("lastName", "Tutorial");
customer2.put("email", "java@simplesolution.dev");
customer2.put("website", "https://simplesolution.dev/java");
Map<String, String> address2 = new HashMap<>();
address2.put("street", "Simple Street");
address2.put("city", "Sample City");
address2.put("country", "Country Name");
customer2.put("address", address2);
list.add(customer2);
Gson gson = new Gson();
String jsonString = gson.toJson(list);
System.out.println("Output JSON String:");
System.out.println(jsonString);
}
}
Output JSON String:
[{"firstName":"Simple","lastName":"Solution","website":"https://simplesolution.dev","address":{"country":"Country Name","city":"Sample City","street":"Simple Street"},"email":"contact@simplesolution.dev"},{"firstName":"Java","lastName":"Tutorial","website":"https://simplesolution.dev/java","address":{"country":"Country Name","city":"Sample City","street":"Simple Street"},"email":"java@simplesolution.dev"}]
Convert a Java object to JSON String
In the next Java program, we learn how to use the Gson.toJson() method to convert objects of a custom defined class into JSON String.
For example, we have Customer and Address class below.
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 following program shows you how to convert a Customer object into JSON String.
JavaObjectToJsonStringExample4.java
import com.google.gson.Gson;
public class JavaObjectToJsonStringExample4 {
public static void main(String... args) {
Customer customer = new Customer();
customer.setFirstName("Simple");
customer.setLastName("Solution");
customer.setEmail("contact@simplesolution.dev");
customer.setWebsite("https://simplesolution.dev");
Address address = new Address();
address.setStreet("Testing Street");
address.setCity("Sample City");
address.setCountry("Country Name");
customer.setAddress(address);
Gson gson = new Gson();
String jsonString = gson.toJson(customer);
System.out.println("Output JSON String:");
System.out.println(jsonString);
}
}
Output JSON String:
{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","website":"https://simplesolution.dev","address":{"street":"Testing Street","city":"Sample City","country":"Country Name"}}
Convert a List of objects to JSON String
In the below Java program to show you how to convert a List of Customer objects into JSON String.
JavaObjectToJsonStringExample5.java
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class JavaObjectToJsonStringExample5 {
public static void main(String... args) {
List<Customer> listOfCustomers = new ArrayList<>();
Customer customer1 = new Customer();
customer1.setFirstName("Simple");
customer1.setLastName("Solution");
customer1.setEmail("contact@simplesolution.dev");
customer1.setWebsite("https://simplesolution.dev");
Address address1 = new Address();
address1.setStreet("Testing Street");
address1.setCity("Sample City");
address1.setCountry("Country Name");
customer1.setAddress(address1);
listOfCustomers.add(customer1);
Customer customer2 = new Customer();
customer2.setFirstName("Java");
customer2.setLastName("Tutorial");
customer2.setEmail("java@simplesolution.dev");
customer2.setWebsite("https://simplesolution.dev/java");
Address address2 = new Address();
address2.setStreet("Testing Street");
address2.setCity("Sample City");
address2.setCountry("Country Name");
customer2.setAddress(address2);
listOfCustomers.add(customer2);
Gson gson = new Gson();
String jsonString = gson.toJson(listOfCustomers);
System.out.println("Output JSON String:");
System.out.println(jsonString);
}
}
Output JSON String:
[{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","website":"https://simplesolution.dev","address":{"street":"Testing Street","city":"Sample City","country":"Country Name"}},{"firstName":"Java","lastName":"Tutorial","email":"java@simplesolution.dev","website":"https://simplesolution.dev/java","address":{"street":"Testing Street","city":"Sample City","country":"Country Name"}}]
Happy Coding 😊