Serialize /Convert Map of object to/from JSON in java (GSON /example)

  • Given the Map of String and object (Map<String,Object>), we would like to convert Map to JSON and vice versa.
  • We will use the Google gson library to achieve serialization and deserialization process.
  • We will create Person class and we will perform the following operations with Person class
    1. Convert Map<String,Person> to JSON string
    2. Convert the JSON string to Map<String,Person>

GSON Maven dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.5</version>
</dependency>

Program – convert Map <k,v> to/from JSON in java(GSON /example)

1.) Person Class:

  • Person class containing firstName, lastName, age etc.
  • We have overloaded toString method to print person information.
package org.learn.gson;
 
public class Person {
    public String firstName;
    public String lastName;
    public int age;
    public String contact;
 
    public Person(String firstName, String lastName, int age, String contact) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.contact = contact;
    }
 
    public String toString() {
        return "[" + firstName + " " + lastName + " " + age + " " + contact + "]";
    }
}

2.) JSONMapConverter Class:

JSONMapConverter is responsible for performing following operations.

  1. Convert Map<String,Person> to JSON string in java.
  2. Convert the JSON string to Map<String,Person> in java.
package org.learn.gson;
 
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
 
public class JSONMapConverter {
    public static void main(String[] args) {
        Gson objGson = new GsonBuilder().setPrettyPrinting().create();
 
        Map<String, Person> mapIdPerson = new HashMap<>();
        mapIdPerson.put("10101001", new Person("Mike", "harvey", 34, "001894536"));
        mapIdPerson.put("20202002", new Person("Nick", "young", 75, "005425676"));
        mapIdPerson.put("30303003", new Person("gary", "hudson", 21, "009654153"));
        mapIdPerson.put("40404004", new Person("gary", "hudson", 55, "00564536"));
 
        Type listType = new TypeToken<Map<String, Person>>() {
        }.getType();
        String mapToJson = objGson.toJson(mapIdPerson);
        System.out.println("1. Map to JSON conversion is : \n");
        System.out.println(mapToJson);
 
        // JSON to Map
        Map<String, Person> jsonToMap = objGson.fromJson(mapToJson, listType);
        System.out.println("2. JSON to Map conversion is :\n");
        jsonToMap.forEach((k, v) -> System.out.println(k + "=" + v));
    }
}

Download Code – Convert Map to JSON Java (GSON /Example)

Output – convert Map <k,v> to/from JSON in java (GSON /example)

1. <strong>Map to JSON conversion is</strong> :
 
{
"30303003": {
"firstName": "gary",
"lastName": "hudson",
"age": 21,
"contact": "009654153"
},
"10101001": {
"firstName": "Mike",
"lastName": "harvey",
"age": 34,
"contact": "001894536"
},
"20202002": {
"firstName": "Nick",
"lastName": "young",
"age": 75,
"contact": "005425676"
},
"40404004": {
"firstName": "gary",
"lastName": "hudson",
"age": 55,
"contact": "00564536"
}
}
2. <strong>JSON to Map conversion is </strong>:
 
30303003=[gary hudson 21 009654153]
10101001=[Mike harvey 34 001894536]
20202002=[Nick young 75 005425676]
40404004=[gary hudson 55 00564536]