Deserialization – How to convert JSON to Map using Jackson API

HOME

This tutorial shows how to convert JSON to JAVA map using Jackson’s data binding. In the previous tutorials, I have explained converting Java Objects/Arrays to JSON String using Jackson API. You can refer below tutorials.

Serialization – How to create JSON Payload from Java Object – Jackson API

Deserialization – How to convert JSON to Java Object using Jackson API

How to create JSON Array Payload using POJO – Jackson API

How to create Nested JSON Object using POJO – Jackson API

To start with, we need to add Jackson Maven Dependency to the POM. Always add the latest version of Jackson dependency to your project.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency

Deserialization converts a stream of bytes into a Java object that we can use in code.

We use Jackson’s ObjectMapper, as we did for serialization, using readValue() to process the input. Also, note our use of Jackson’s TypeReference, which we’ll use in all of our deserialization examples to describe the type of our destination Map.

   @Test
	public void DeserializationMapTest() throws JsonProcessingException {

		String employeeString = 
                     "{\r\n" 
                    + "  \"firstName\" : \"Deserialization\",\r\n"
				    + "  \"lastName\" : \"Test\",\r\n" 
                    + "  \"age\" : 25,\r\n" 
                    + "  \"salary\" : 50000.0,\r\n"
				    + "  \"designation\" : \"Manager\",\r\n" 
                    + "  \"contactNumber\" : \"+918882211111\",\r\n"
				    + "  \"emailId\" : \"abc@test.com\",\r\n" 
                    + "  \"gender\" : \"female\",\r\n"
				    + "  \"maritalStatus\" : \"single\"\r\n" 
                    + " }";

		Map<String, String> resultMap = new HashMap<String, String>();
		ObjectMapper mapper = new ObjectMapper();

		// Converting a JSON to Java Map
		try {
			resultMap = mapper.readValue(employeeString, new TypeReference<HashMap<String, String>>() {
			});
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

		System.out.println("Output Map :" + resultMap);
	}

Output

We can get the JSON from a file and convert it to a Hash Map too. Below is an example of the same.

   @Test
	public void PayloadFromFile() {

		String userDir = System.getProperty("user.dir");
		ObjectMapper mapper = new ObjectMapper();

		// Converting Employee JSON string to Employee class object
		Map<String, Object> empMap;
		try {
			empMap = mapper.readValue(new File(userDir + "\\src\\test\\resources\\JSONFromMap.json"),
					new TypeReference<Map<String, Object>>() {
					});

			System.out.println("Gender : " + empMap.get("gender"));
			System.out.println("DOB : " + empMap.get("DOB"));
			System.out.println("Name : " + empMap.get("name"));
			System.out.println("ContactNumber : " + empMap.get("contactNumber"));
			System.out.println("SkillSet : " + empMap.get("skillset"));
		} catch (StreamReadException e) {
			e.printStackTrace();
		} catch (DatabindException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

Output

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s