
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert JSON to/from Map using Jackson library in Java?
JSON is a format that is used to exchange data between a server and a client. It is lightweight and easy to read and write. In Java, we can convert JSON to/from Map using various libraries. In this article, we will discuss how to convert JSON to/from Map using the Jackson library.
The Jackson is a library for Java, and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class.
To use the Jackson library, we need to add the Jackson library to our project. If you are using Maven, add this to your pom.xml
file:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency>
If you are not using Maven, add a jar file to your project. You can download the jar file from here.
Convert JSON to Map
To convert JSON to a Map using the Jackson library, we can use the readValue() method of ObjectMapper class. The readValue() method takes two parameters: the JSON string and the type of the object to be converted to. In this case, we will pass Map<String, Object>
as the type of the object.
Steps to convert JSON to Map using Jackson library:
- First of all, import the ObjectMapper class from the com.fasterxml.jackson.databind package.
- Then create a JSON string.
- Then we will create an instance of ObjectMapper class.
- Next, we will convert the JSON string to a Map using the readValue() method of ObjectMapper.
- Finally, we will print the Map.
Example
Following is the code to convert JSON to a Map using the Jackson library:
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; import java.io.IOException; public class JsonToMap { public static void main(String[] args) { // Create a JSON string String jsonString = "{"name":"Ansh", "age":23, "city":"New York"}"; // Create an ObjectMapper instance ObjectMapper objectMapper = new ObjectMapper(); try { // Convert JSON string to Map Map<String, Object> map = objectMapper.readValue(jsonString, Map.class); // Print the Map System.out.println("Map: " + map); } catch (IOException e) { e.printStackTrace(); } } }
Following is the output of the above code:
Map: {name=Ansh, age=23, city=New York}
Convert Map to JSON
To convert a Map to JSON using the Jackson library, we can use the writeValueAsString() method of the ObjectMapper class. The writeValueAsString() method takes one parameter: the Map to be converted to JSON. Steps to convert Map to JSON using Jackson library:
- First, import the com.fasterxml.jackson.databind.ObjectMapper class.
- Then create a Map.
- Then we will create an instance of ObjectMapper class.
- Next, we will convert the Map to JSON using the writeValueAsString() method of ObjectMapper.
- Finally, we will print the JSON string.
Example
Following is the code to convert a Map to JSON using the Jackson library:
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; import java.io.IOException; public class MapToJson { public static void main(String[] args) { // Create a Map Map<String, Object> map = new HashMap<>(); map.put("name", "Ansh"); map.put("age", 23); map.put("city", "New York"); // Create an ObjectMapper instance ObjectMapper objectMapper = new ObjectMapper(); try { // Convert Map to JSON string String jsonString = objectMapper.writeValueAsString(map); // Print the JSON string System.out.println("JSON String: " + jsonString); } catch (IOException e) { e.printStackTrace(); } } }
Following is the output of the above code:
JSON String: {"name":"Ansh","age":23,"city":"New York"}