Here I am going to show you how to convert a JSON to Java Map object using Jackson.

JSON to Java Map :

In the previous tutorials we have seen how to convert a Java object to JSON string, you can see the required Jackson dependencies there.

Here I am going to create a JSON string and convert it into Java’s java.util.Map object.


package com.onlinetutorialspoint.json;

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JSON_String_TO_Map {
    public static void main(String[] a) {

        ObjectMapper mapperObj = new ObjectMapper();
        Map<String,String> jsonMap = new HashMap<String,String>(); 
        try {
            jsonMap = mapperObj.readValue(jsonString(),
                    new TypeReference<HashMap<String,String>>(){});
            System.out.println(jsonMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String jsonString() {
        String str = "{\"studentId\": 1001, \"studentName\": \"Rajesh\","
                + "\"roleNumber\": \"106\",\"standard\": \"11th\"}";
        return str;

    }
}

Output:

{studentId=1001, standard=11th, studentName=Rajesh, roleNumber=106}

Happy Learning 🙂