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.
Arafat
how to get the value of “age” only.Thanks
Shan
how to do it for the json array?
[{“age”:29,”name”:”shan”}, {“age”:30,”name”:”more”}]
LordThor
How do you handle the special characters like ” in the json Mapping
Lokesh Gupta
Use character escaping using backslash.
Brilan-2014
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.
kheem
hi lokesh,,
tell me the best way to handle the jvm memory issues.
Poly
Good article and i have to say thank you , im learning lots of stuff in here happy learning