⌛️ 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"}
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Find MAC address of your laptop device - HowTos
- Deep Dive: Why avoid java.util.Date and Calendar Classes - Java
- List of jars required for Struts2 project - Java
- 'pwd' is not recognized as an internal or external command, operable program or batch file. [Windows] - Bash
- Install GitHub Command Line Tool on Mac - Git
- Java 8 Display time in 12 hour AM PM format - Java
- W3 : character data is not allowed here html validation error - Html
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio