Serialization – How to convert Map to JSON string using Jackson API

HOME

This tutorial shows how to convert Java map to JSON string 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 to the below tutorials.

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

How to create JSON Array Payload using POJO – Jackson API

How to create Nested JSON Object using POJO – Jackson API

To start off, add the latest Jackson dataformat Maven dependency to the project.

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.13.0</version>
</dependency>

Sample JSON

{
  "skillset" : [ 
                 "Java",
                 "Teradata",
                 "Python", 
                 "Power BI" 
               ],
  "gender" : "female",
  "DOB" : "12-02-1985",
  "name" : "Vibha Singh",
  "contactNumber" : "+919999988822",
  "employeeId" : "10342256",
  "location" : "Dublin",
  "emailId" : "abc@test.com",
  "salary" : "75000.0"
}

First, we will populate a Map, then convert them into JSON and later write that JSON to a file.

	@Test
	public void SerializationMapTest() {

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

		inputMap.put("employeeId", "10342256");
		inputMap.put("name", "Vibha Singh");
		inputMap.put("DOB", "12-02-1985");
		inputMap.put("salary", "75000.0");
		inputMap.put("location", "Dublin");
		inputMap.put("contactNumber", "+919999988822");
		inputMap.put("emailId", "abc@test.com");
		inputMap.put("gender", "female");

		List<String> skillset = new ArrayList<String>();

		skillset.add("Java");
		skillset.add("Teradata");
		skillset.add("Python");
		skillset.add("Power BI");

		inputMap.put("skillset", skillset);

		// Converting map to a JSON payload as string
		try {
			String employeePrettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputMap);
			System.out.println(employeePrettyJson);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}

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

        //Writing JSON on a file
		try {
			mapper.writerWithDefaultPrettyPrinter()
					.writeValue(new File(userDir + "\\src\\test\\resources\\JSONFromMap.json"), inputMap);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

As mentioned above, the new JSON is saved in a file and placed under src/test/resources.

Below is the file with JSON.

Congratulations, we are done. We have successfully created a JSON using HashMap.

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 )

Google photo

You are commenting using your Google 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