How to Convert Java Object to JSON
In this tutorial we show how to convert a Java object to JSON using two libraries, Jackson and Gson.
We will use a Person class which is just a standard POJO. Once we create a person object, we can then convert it to JSON using different libraries.
Person.java
import java.util.List;
import java.util.Map;
public class Person {
String name;
Integer age;
List<String> hobbies;
Map<String, String> languages;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getLanguages() {
return languages;
}
public void setLanguages(Map<String, String> languages) {
this.languages = languages;
}
}
Convert Java to JSON Using Jackson
We can use the ObjectMapper
class of the Jackson library to convert a Java object to JSON.
The ObjectMapper
class has different methods to write JSON to file or to write the JSON as String.
In the following example, we write to a file and also use the pretty print function to display a formatted JSON.
But first, get Jackson library from Maven and put as dependency in the pom.xml
file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>
ConvertJavaObjectToJson.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertJavaObjectToJson {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Person person = newPerson();
try {
mapper.writeValue(new File("person.json"), person);
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);
System.out.println(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Person newPerson() {
Person person = new Person();
List<String> hobbies = new ArrayList<>();
hobbies.add("Football");
hobbies.add("Cooking");
hobbies.add("Swimming");
Map<String, String> languages = new HashMap<>();
languages.put("French", "Beginner");
languages.put("German", "Intermediate");
languages.put("Spanish", "Advanced");
person.setName("David");
person.setAge(30);
person.setHobbies(hobbies);
person.setLanguages(languages);
return person;
}
}
Output:
{
"name" : "David",
"age" : 30,
"hobbies" : [ "Football", "Cooking", "Swimming" ],
"languages" : {
"French" : "Beginner",
"German" : "Intermediate",
"Spanish" : "Advanced"
}
}
Also a file person.json
is created in the project workspace.
Convert Java to JSON Using Gson
Get the dependency
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Then
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertJavaObjectToJson {
public static void main(String[] args) {
Gson gson = new Gson();
Person person = newPerson();
String jsonString = gson.toJson(person);
System.out.println(jsonString);
}
private static Person newPerson() {
Person person = new Person();
List<String> hobbies = new ArrayList<>();
hobbies.add("Football");
hobbies.add("Cooking");
hobbies.add("Swimming");
Map<String, String> languages = new HashMap<>();
languages.put("French", "Beginner");
languages.put("German", "Intermediate");
languages.put("Spanish", "Advanced");
person.setName("David");
person.setAge(30);
person.setHobbies(hobbies);
person.setLanguages(languages);
return person;
}
}
Output