How to Convert JSON to / from Java Map using JACKSON

In this Jackson example we will learn how to convert JSON to Java Map and vice versa (Convert Java Map to JSON) using Jackson API. Since JSON will be in the form of Key Value pairs it can be easily converted to Java map.

Folder Structure:

Convert JSON to Java

  1. Create a new Java Project  JacksonJSONTutorial” and create a package for our src files com.javainterviewpoint
  2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.

jackson-core.jar
jackson-annotations.jar
jackson-databind.2.8.4.jar

if you are running on maven add the below dependency to your pom.xml

 <dependencies>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.8.4</version>
 </dependency>
</dependencies>
  1. Create the Java classes JSONFileToMap.java, JSONToMap.java,MapToJSON.java and MapToJSONFile.java under  com.javainterviewpoint folder.

Convert JSON to Java Map

Read JSON String and convert JSON String to Java Map

package com.javainterviewpoint;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONToMap
{
    public static void main(String args[])
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = "{\"name\":\"JavaInterviewPoint\", \"department\":\"blogging\"}";
            
            Map<String, Object> jsonMap = new HashMap<String, Object>();
            
            // convert JSON string to Map
            jsonMap = mapper.readValue(jsonString,
                    new TypeReference<Map<String, String>>(){});
            
            System.out.println(jsonMap);
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
        }

    }
}

Output :

{name=JavaInterviewPoint, department=blogging}

Convert Java Map to JSON String

package com.javainterviewpoint;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MapToJSON
{
    public static void main(String args[])
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = "";
            
            Map<String, Object> jsonMap = new HashMap<String, Object>();
            jsonMap.put("name","JavaInterviewPoint");
            jsonMap.put("department","IT");
           
            // convert Map to JSON String
            jsonString = mapper.writeValueAsString(jsonMap);
            
            System.out.println(jsonString);
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
        }

    }
}

Output:

{"name":"JavaInterviewPoint","department":"IT"}

Convert JSON File to Java Map

package com.javainterviewpoint;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONFileToMap
{
    public static void main(String args[])
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();

            // read JSON from a file
            Map<String, Object> jsonMap = mapper.readValue(new File(
                    "D:\\test.json"),
                    new TypeReference<Map<String, Object>>(){});
            
            System.out.println("*** JSON File Contents ***");
            System.out.println("Name : "+jsonMap.get("name"));
            System.out.println("Name : "+jsonMap.get("department"));
            System.out.println("Name : "+jsonMap.get("age"));
            
        } catch (IOException ie)
        {
            ie.printStackTrace();
        }

    }
}

Output:

*** JSON File Contents ***
Name : JavaInterviewPoint
Name : IT
Name : 22

Convert Java Map to JSON File

package com.javainterviewpoint;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MapToJSONFile
{
    public static void main(String args[])
    {
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            
            Map<String, Object> jsonMap = new HashMap<String, Object>();
            jsonMap.put("name","JavaInterviewPoint");
            jsonMap.put("department","IT");
            jsonMap.put("age",22);
            
            // convert map to JSON File
            mapper.writeValue(new File("D:\\test.json"),jsonMap);
            
            System.out.println("File Write Completed");
        }
        catch(IOException ie)
        {
            ie.printStackTrace();
        }
    }
}

Output :
After execution of the above class, we will be having a test.json created with the below content

{"name":"JavaInterviewPoint","department":"IT","age":22}

asdf

Leave a Reply

Your email address will not be published. Required fields are marked *