Gson – convert HashMap to JSON example shows how to convert a Java HashMap object to JSON using the Gson library in Java. The example also shows how to convert HashMap containing custom objects to JSON.
How to convert a Map/HashMap to JSON?
Converting a Java Map or HashMap to JSON can be done using the toJson
method of the Gson class. The below given example converts a simple map object to a JSON representation using the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javacodeexamples.gsonexamples; import java.util.HashMap; import java.util.Map; import com.google.gson.Gson; public class GsonHashMapToJSONExample { public static void main(String[] args) { //map containing an employee values Map<String, Object> mapValues = new HashMap<String, Object>(); mapValues.put("firstName", "Raj"); mapValues.put("lastName", "Kumar"); mapValues.put("age", 34); //create new Gson object Gson gson = new Gson(); /* * Use the toJson method to * convert map to JSON */ String json = gson.toJson(mapValues); System.out.println(json); } } |
Output
1 |
{"firstName":"Raj","lastName":"Kumar","age":34} |
As we can see from the output, it created a JSON object from the map object. Also, note that since the firstName and lastName values were strings in the HashMap object, there are wrapped in the double quotes in the JSON, while since the age was a number in the map, it is not wrapped in the double quotes in the JSON.
The Gson library can also convert a map of custom objects to JSON using the same toJson
method. Consider the below given example that converts the HashMap of custom Student objects to the JSON.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.javacodeexamples.gsonexamples; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonHashMapToJSONExample { public static void main(String[] args) { //map containing student objects Map<String, Student> mapStudents = new HashMap<String, Student>(); mapStudents.put("1", new Student(1, "Raj", Arrays.asList("Maths", "Chemistry"))); mapStudents.put("2", new Student(2, "Bob", Arrays.asList("Physics", "Biology"))); mapStudents.put("3", new Student(3, "Raj", Arrays.asList("Music", "English"))); //create new Gson object with pretty print Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); /* * Use the toJson method to * convert map to JSON */ String json = gson.toJson(mapStudents); System.out.println(json); } } class Student{ private int rollNumber; private String name; private List<String> subjects; public Student(int rollNumber, String name, List<String> subjects) { this.rollNumber = rollNumber; this.name = name; this.subjects = subjects; } public List<String> getSubjects() { return subjects; } public void setSubjects(List<String> subjects) { this.subjects = subjects; } public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{ "1": { "rollNumber": 1, "name": "Raj", "subjects": [ "Maths", "Chemistry" ] }, "2": { "rollNumber": 2, "name": "Bob", "subjects": [ "Physics", "Biology" ] }, "3": { "rollNumber": 3, "name": "Raj", "subjects": [ "Music", "English" ] } } |
As we can see from the output, the Gson library automatically converted the “subjects” Java list to a JSON array.
Please let me know your views in the comments section below.