In this post, we will learn how to convert Java Map into a JSON using 3 different libraries. As stated, we are going to use Gson, Jackson, and org.json libraries to achieve our objective one at a time. All three are quite famous libraries and used quite often.
Convert Java Map to JSON...!!! Click To TweetIn this tutorial, we are going to cover the below topics:
- Map to JSON using Gson
- Map to JSON using Jackson
- Map to JSON using org.json
Let’s begin:
1. Convert Java Map to JSON using Gson
In order to work with Gson, we first need to add it’s dependency into the Project.
Maven dependencies
If you are using Maven as a build tool in your project, then add the following dependency in your pom.xml file.
1 2 3 4 5 6 7 | <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> </dependencies> |
else, access the below link and download & add the jar into your project classpath:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.lang.reflect.Type; import java.util.SortedMap; import java.util.TreeMap; import org.junit.jupiter.api.Test; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class Convert_JavaMap_To_Json_Using_Gson { @Test public void convertJavaMapToJsonUsingGson() { SortedMap <String, Integer> objects = new TreeMap <String, Integer> (); objects.put("Key1", 1); objects.put("Key2", 2); objects.put("Key3", 3); Gson gsonObj = new Gson(); Type gsonTypeObj = new TypeToken <>(){}.getType(); String jsonString = gsonObj.toJson(objects, gsonTypeObj); System.out.println(jsonString); } } |
Output:
1 | {"Key1":1,"Key2":2,"Key3":3} |
2. Convert Java Map to JSON using Jackson
In order to work with Jackson, we first need to add it’s dependency into the Project.
Maven dependencies
If you are using Maven as a build tool in your project, then add the following dependency in your pom.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 | <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.2</version> </dependency> </dependencies> |
else, download & add the following Jars into your project classpath:
- jackson-core
- jackson-databind
In order to download jars, access below link and search for above-mentioned jar names and download the appropriate one from the search result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Convert_JavaMap_To_Json_Using_Jackson { @Test public void convertJavaMapToJsonUsingJackson() throws JsonProcessingException { Map <String, Integer> objects = new HashMap <String, Integer> (); objects.put("Key1", 1); objects.put("Key2", 2); objects.put("Key3", 3); ObjectMapper objectMapperObj = new ObjectMapper(); String jsonString = objectMapperObj.writeValueAsString(objects); System.out.println(jsonString); } } |
Output:
1 | {"Key2":2,"Key1":1,"Key3":3} |
3. Convert Java Map to JSON using org.json
In order to work with org.json, we first need to add it’s dependency into the Project.
Maven dependencies
If you are using Maven as a build tool in your project, then add the following dependency in your pom.xml file.
1 2 3 4 5 6 7 | <dependencies> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20190722</version> </dependency> </dependencies> |
else, access the below link, it will download the jar in a zip file, extract it and & add this jar into your project classpath:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.HashMap; import java.util.Map; import org.json.JSONObject; import org.junit.jupiter.api.Test; public class Convert_JavaMap_To_Json_Using_OrgJson { @Test public void convertJavaMapToJsonUsingOrgJson() { Map <String, Integer> objects = new HashMap <> (); objects.put("Key1", 1); objects.put("Key2", 2); objects.put("Key3", 3); JSONObject jsonString = new JSONObject(objects); System.out.println(jsonString); } } |
Output:
1 | {"Key1":1,"Key2":2,"Key3":3} |
Convert Java Map to JSON...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Double the even / odd numbers of a specified ArrayList using Streams
- Double the numbers of specified ArrayList using Streams
In section 2. Convert Java Map to JSON using Jackson, you are still using GSON code snippet instead of Jackson, wrong copy-paste?
Hi Serguei,
Sorry for the inconvenience and thanks for pointing that out. Yes, that was the copy-paste issue. Well, I’ve fixed it now. Please take a look.
Thanks