Convert JSON String to Map and Map to JSON String with Jackson

In this section, we will show you how to convert JSON String to Map and Map to JSON String with Jackson.

Note


Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa.

Note


JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging.

JSON is built on two structures: 

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. 
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

1. Download Jackson

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


In Jackson, we can use mapper.readValue(json, Map.class) to convert a JSON string to a Map.

2. JSON string to Map

Main.java

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;

public class Main {
public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"sibin\", " +
"\"email\":\"sibin@gmail.in\"}";

try {

// convert JSON string to Map
Map<String, String> map1 = mapper
.readValue(json, Map.class);


Map<String, String> map2 = mapper.readValue(json,
new TypeReference<Map<String, String>>() {});

System.out.println(map1);
System.out.println(map2);

} catch (IOException e) {
e.printStackTrace();
}

}
}


Console Output:
{name=sibin, email=sibin@gmail.in}
{name=sibin, email=sibin@gmail.in}

3. Map to JSON string

Main.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

public class Main {

public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();

Map<String, String> map = new HashMap<>();
map.put("name", "sibin");
map.put("email", "sibin@gmail.in");

try {

// convert map to JSON string
String json1 = mapper.writeValueAsString(map);

System.out.println(json1); // compact-print

// convert map to JSON string - pretty print
String json2 = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);

System.out.println(json2);

} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}


Console Output:
{"name":"sibin","email":"sibin@gmail.in"}
{
  "name" : "sibin",
  "email" : "sibin@gmail.in"
}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete