Since JSON format is essentially a key-value pair grouping, JSON can easily be converted to/from Java maps. This post is about JSON to MAP conversion. Let’s get going.
Step 1: Include JACKSON dependency in pom.xml
There are several jar in JACKSON library. For this example, we are only using databind.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.websystique.xml</groupId> <artifactId>JsonJacksonMapMappingDemo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>JsonJacksonMapMappingDemo</name> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> </dependencies> </project>
Step 2: Convert Java Map to JSON and write JSON to a file
Populate a Map, convert into JSON and write that JSON to a file
package com.websystique.json.jackson; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonJacksonMapMappingDemo { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> carMap = new HashMap<String, Object>(); carMap.put("car", "Audi"); carMap.put("price", "30000"); carMap.put("model", "2010"); List<String> colors = new ArrayList<String>(); colors.add("Grey"); colors.add("White"); colors.add("Black"); carMap.put("colors", colors); /** * Convert Map to JSON and write to a file */ try { mapper.writeValue(new File("result.json"), carMap); } catch (Exception e) { e.printStackTrace(); } } }
Now open result.json, you should see following output
{"car":"Audi","model":"2010","price":"30000","colors":["Grey","White","Black"]}
Step 3: Convert JSON to Java Map
Read JSON string from a file and convert JSON to Java Map
package com.websystique.json.jackson; import java.io.File; import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonJacksonMapMappingDemo { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); /** * Read JSON from a file into a Map */ try { Map<String, Object> carMap = mapper.readValue(new File( "result.json"), new TypeReference<Map<String, Object>>() { }); System.out.println("Car : " + carMap.get("car")); System.out.println("Price : " + carMap.get("price")); System.out.println("Model : " + carMap.get("model")); System.out.println("Colors : " + carMap.get("colors")); } catch (Exception e) { e.printStackTrace(); } } }
Below is the output:
Car : Audi Price : 30000 Model : 2010 Colors : [Grey, White, Black]
That’s it. In the next post we will learn about Jackson Tree model api.
References
If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.