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...!!! Share on X

In this tutorial, we are going to cover the below topics:

  1. Map to JSON using Gson
  2. Map to JSON using Jackson
  3. 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.

<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:

Gson Jar

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:

{"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.

<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.

Jackson Jar

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:

{"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.

<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:

org.json Jar

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:

{"Key1":1,"Key2":2,"Key3":3}
Convert Java Map to JSON...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts