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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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 our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close