Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Convert Java Map To / From JSON using Jackson example

With this example we are going to see how to convert a java.util.Map Object to /from JSON representation using the Jackson parser. Converting Map to / from JSON comes naturally because they have a pretty similar representation and usage. Take a look at the precious Jackson tutorial where we showed how to convert a user defined Java Object to / from JSON.
 
 
 
 
 
 

1. Convert Map Object to JSON represenation

MapToJSONExample.java:

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class MapToJSONExample {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\mapExample.json";

	public static void main(String[] args) {

		ObjectMapper objectMapper = new ObjectMapper();

		Map<String, Object> mapObject = new HashMap<String, Object>();

		mapObject.put("domain", "JavaCodeGeeks.com");
		mapObject.put("interest", "Java");

		mapObject.put("Members", 400);

		List<Object> myList = new ArrayList<Object>();

		myList.add("Jonh");
		myList.add("Jack");
		myList.add("James");

		mapObject.put("names", myList);

		try {
			objectMapper.writeValue(new File(jsonFilePath), mapObject);

		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

mapExample.json:

{"names":["Jonh","Jack","James"],"interest":"Java","domain":"JavaCodeGeeks.com","Members":400}

2. Convert JSON to Map Object

JSONToMapExample.java:

package com.javacodegeeks.java.core;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JSONToMapExample {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\mapExample.json";

	public static void main(String[] args) {

		ObjectMapper mapper = new ObjectMapper();

		try {

			File jsonFile = new File(jsonFilePath);
			Map<String, Object> mapObject = mapper.readValue(jsonFile,
					new TypeReference<Map<String, Object>>() {
					});

			System.out.println("domain   : " + mapObject.get("domain"));

			System.out.println("interest : " + mapObject.get("interest"));

			System.out.println("Members  : " + mapObject.get("Members"));

			System.out.print("Names    : ");

			List<String> list = (List<String>) mapObject.get("names");

			for (String name : list) {
				System.out.print(name + " ");
			}

		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

output:

domain   : JavaCodeGeeks.com
interest : Java
Members  : 400
Names    : Jonh Jack James 

 
This was an example on how to convert Java Map To / From JSON using Jackson.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close