HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Jackson / Jackson – Convert JSON to Map and Map to JSON

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Arafat

    February 28, 2020

    how to get the value of “age” only.Thanks

  2. Shan

    July 28, 2019

    how to do it for the json array?
    [{“age”:29,”name”:”shan”}, {“age”:30,”name”:”more”}]

  3. LordThor

    May 10, 2018

    How do you handle the special characters like ” in the json Mapping

    • Lokesh Gupta

      May 10, 2018

      Use character escaping using backslash.

  4. Brilan-2014

    January 13, 2016

    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.

  5. kheem

    June 25, 2014

    hi lokesh,,
    tell me the best way to handle the jvm memory issues.

  6. Poly

    June 16, 2014

    Good article and i have to say thank you , im learning lots of stuff in here happy learning

Comments are closed on this article!

Search Tutorials

JAXB Tutorial

  • JAXB – Annotations
  • JAXB – @XmlRootElement
  • JAXB – @XmlElementWrapper
  • JAXB – Marshaller
  • JAXB – Unmarshaller
  • JAXB – Convert XML to Java Object
  • JAXB – Convert JSON to Java Object
  • JAXB – Convert Java Object to XML
  • JAXB – Convert Java Object to JSON
  • JAXB – Map
  • JAXB – List and Set
  • JAXB – Generate Schema
  • JAXB – Schema Validation
  • JAXB – JAXBException
  • JAXB – IllegalAnnotationExceptions
  • JAXB – Marshal without @XmlRootElement
  • JAXB – Unmarshal without @XmlRootElement
  • Jackson 2 – Object from/to JSON
  • Jackson – Object from/to json
  • Jackson – JSON from/to Map

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces