Site icon

Convert Map to/from JSON string in java (jackson objectmapper/ example)

Jackson objectMapper maven Dependencies

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

Serialize or Convert Map to/from JSON in java (jackson objectmapper)

1.) Person Class:

package org.learn;

public class Person {
	public String firstName;
	public String lastName;
	public int age;
	public Person() {		
	}
	public Person(String firstName, String lastName,
				int age) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}
	public String toString() {
	    return "[" + firstName + " " + lastName +
		       " " + age +"]";
	}
}

2.) JSONMapConverter Class:

  1. Convert Map<String,Person> to JSON in java.
  2. Convert the JSON to Map<String,Person> in java.
package org.learn;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JSONMapConverter 
{
    public static void main( String[] args ) throws IOException
    {
    	ObjectMapper objectMapper = new ObjectMapper();
    	//Set pretty printing of json
    	objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

    	//Define map which will be converted to JSON
    	Map<String,Person>mapIdPerson  = new HashMap<>();
    	mapIdPerson.put("10101001", new Person("Mike", "harvey", 34));
    	mapIdPerson.put("20202002", new Person("Nick", "young", 75));
    	mapIdPerson.put("30303003", new Person("gary", "hudson", 21));
    	mapIdPerson.put("40404004", new Person("gary", "hudson", 55));
    	
    	//1. Convert Map to JSON
    	String mapToJson = objectMapper.writeValueAsString(mapIdPerson);
    	System.out.println("1. Convert Map to JSON :");
    	System.out.println(mapToJson);
    	
    	//2. JSON to Map
    	//Define Custom Type reference for map type
    	TypeReference<Map<String, Person>> mapType = new TypeReference<Map<String,Person>>() {};
    	Map<String,Person> jsonToMap = objectMapper.readValue(mapToJson, mapType);
    	System.out.println("\n2. Convert JSON to Map :");
    	
    	//Print map output using Java 8
    	jsonToMap.forEach((k, v) -> 
    						System.out.println(k + "=" + v));
    }
}

Download Example Code – Convert Serialize Jackson Map to JSON Java

Output – Serialize/convert Map to/from JSON in java (jackson objectmapper)

1. Convert Map to JSON :
{
  "30303003" : {
    "firstName" : "gary",
    "lastName" : "hudson",
    "age" : 21
  },
  "10101001" : {
    "firstName" : "Mike",
    "lastName" : "harvey",
    "age" : 34
  },
  "20202002" : {
    "firstName" : "Nick",
    "lastName" : "young",
    "age" : 75
  },
  "40404004" : {
    "firstName" : "gary",
    "lastName" : "hudson",
    "age" : 55
  }
}

2. Convert JSON to Map :
30303003=[gary hudson 21]
10101001=[Mike harvey 34]
20202002=[Nick young 75]
40404004=[gary hudson 55]
Exit mobile version