⌛️ 4 minute read! Difficulty: Medium

Step 1: Make sure to add Jackson dependency in your Java Gradle/Maven Project
Gradle:implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
Step 3: Java Collection Map to JSON String
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class JavaMapToJsonString {
public static void main(String[] args) {
try {
Map<String,String> employeeMap1 = new HashMap<>();
employeeMap1.put("empName","Mike");
employeeMap1.put("empAge","34");
employeeMap1.put("empDept","Finance");
Map<String,String> employeeMap2 = new HashMap<>();
employeeMap2.put("empName","Harry");
employeeMap2.put("empAge","33");
employeeMap2.put("empDept","Insurance");
ObjectMapper objectMapper = new ObjectMapper();
String empJson1 = objectMapper.writeValueAsString(employeeMap1);
String empJson2 = objectMapper.writeValueAsString(employeeMap2);
System.out.println(empJson1);
System.out.println(empJson2);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
{"empName":"Mike","empAge":"34","empDept":"Finance"}
{"empName":"Harry","empAge":"33","empDept":"Insurance"}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Fix: EOFError Exception in Python - Python
- [Maven] Multiple annotations found at this line pom.xml CoreException, ArtifactResolutionException - Java
- How to exit from nano command - Linux
- How to save webpage as pdf using macOS Safari - MacOS
- How to Pretty Print JSON in PHP - PHP
- Get MD5 Hash as Checksum of a String in Python - Python
- Python 3.x : How to Convert String to Bytes - Python
- How to create alias in macOS - MacOS