Jackson

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.

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. 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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button