In the following code snippet we will convert java.util.Map
object into JSON string and convert back the JSON string into Java Map
object. In this example we will be using the Jackson library.
To convert from Map
to JSON string the steps are:
- Create a map of string keys and values.
- Create an instance of Jackson
ObjectMapper
. - To convert map to JSON string we call the
writeValueAsString()
method and pass the map as argument.
// Converting Map to JSON
String json = null;
try {
json = mapper.writeValueAsString(colours);
System.out.println("json = " + json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Now, to convert from JSON string back to map we can do it in the following steps:
- Create a JSON string, in this case we use the one converted from the
colours
map. - Create an instance of Jackson
ObjectMapper
. - Call the mapper’s
readValue()
method with JSON string and an empty instance ofTypeReference
as arguments.
// Converting JSON to MAP
try {
Map<String, String> newColours =
mapper.readValue(json, new TypeReference<>() {});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
And here is the complete code snippet.
package org.kodejava.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public class MapToJson {
public static void main(String[] args) {
Map<String, String> colours = new HashMap<>();
colours.put("BLACK", "#000000");
colours.put("RED", "#FF0000");
colours.put("GREEN", "#008000");
colours.put("BLUE", "#0000FF");
colours.put("YELLOW", "#FFFF00");
colours.put("WHITE", "#FFFFFF");
ObjectMapper mapper = new ObjectMapper();
// Converting Map to JSON
String json = null;
try {
json = mapper.writeValueAsString(colours);
System.out.println("json = " + json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// Converting JSON to MAP
try {
Map<String, String> newColours =
mapper.readValue(json, new TypeReference<>() {});
System.out.println("Map:");
for (var entry : newColours.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Running the code snippet above will print the following output:
json = {"RED":"#FF0000","WHITE":"#FFFFFF","BLUE":"#0000FF","BLACK":"#000000","YELLOW":"#FFFF00","GREEN":"#008000"}
Map:
RED = #FF0000
WHITE = #FFFFFF
BLUE = #0000FF
BLACK = #000000
YELLOW = #FFFF00
GREEN = #008000
Maven Dependencies
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024