Jackson – Convert JSON to Map and Map to JSON

In Jackson json map example example, we will learn to convert json to map object and then we will learn to convert java map to json.

1. Jackson maven dependency

Include Jackson 2 dependency in your application project. Do not forget to check for latest dependency at maven site.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.6</version>
</dependency>

2. Jackson convert Map to JSON

Java program to convert Map to JSON is as follows. I am using HashMap here.

package com.howtodoinjava.jackson2.example;

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

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MapToJSON 
{
	public static void main(String[] args) 
	{
		HashMap<String, Object> hashmap = new HashMap<String, Object>();

		hashmap.put("id", 11);
		hashmap.put("firstName", "Lokesh");
		hashmap.put("lastName", "Gupta");

		ObjectMapper mapper = new ObjectMapper();
		try 
		{
			//Convert Map to JSON
			String json = mapper.writeValueAsString(hashmap);
			
			//Print JSON output
			System.out.println(json);
		} 
		catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Program Output.

{"id":11,"lastName":"Gupta","firstName":"Lokesh"}

3. Jackson convert JSON to Map

Java program to convert JSON to Map is as follows. I am using HashMap here.

package com.howtodoinjava.jackson2.example;

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

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

public class JSONToMap
{
	public static void main(String[] args) 
	{
		String json = "{\"id\":1,\"name\":\"Lokesh Gupta\",\"age\":34,\"location\":\"India\"}";
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		ObjectMapper mapper = new ObjectMapper();
		try 
		{
			//Convert Map to JSON
			map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
			
			//Print JSON output
			System.out.println(map);
		} 
		catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Program Output.

{id=1, name=Lokesh Gupta, age=34, location=India}

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

7 thoughts on “Jackson – Convert JSON to Map and Map to JSON”

  1. Amazing article, so it’s clear so many bad habits on me when coding or the thinking…thank you so much for offer this and hope more from U.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.