How to convert map to JSON string in Java

In this tutorial, we will learn how to convert Map to JSON String.

For this, first of all, we will learn

What is the map?

Answer. The map contains the values on the basis of keys ie. key-value pair (as a dictionary in python). Each pair of key and value is known as the entry. The map contains unique values of keys. The map does not allow duplicate keys, but it can allow duplicate values.

There are

  • Two interfaces for implementing maps( ie. Map and SortedMap)
  • Three classes for implementing maps(ie. HashMap, LinkedHashMap, TreeSet) as Map is an interface so we cannot create the object of Map, therefore, we use these classes.

What is the JSON string?

Answer. Full-Form of JSON is JavaScript object notation. It is a lightweight format, therefore we can say that it is more preferable to use (lighter than XML), and as a result, we use it for data-interchange.

JSON is easy to read and write and language-dependent.

Java program to convert Map to JSON String

  1. First of all, we will import all the packages required ie.
  • java.util.*;
  • java.io.IOException;
  • org.codehaus.jackson.map.ObjectMapper;

We use HashMap for creating a key-value pair ie. the map.

HashMap allows null keys(  but only once ), the null value( multiple times). We use

  •  java.io.IOException for handling input-output Exceptions. We use a try-catch block for handling exceptions.
  • org.codehaus.jackson.map.ObjectMapper for converting java objects and matching JSON constructs ie. ( javascript object notation). For using org.codehaus.jackson.map.ObjectMapper we need to add the Jackson-all-1.9.0 jar file.

Link to download jar-file:: https://www.codespeedy.com/wp-content/uploads/2020/01/jackson-jaxrs-1.9.0.jar_.zip 

2. Then we will create a new class of any name.

3. After this, we will create the main method in our class.

Java Code

Below is given our desired Java code:

import java.io.IOException;
import java.util.*;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JavaApplication27 {
 
    public static void main(String a[]){
         
        ObjectMapper objmapper = new ObjectMapper();
        Map<String, String> mapobj = new HashMap<String, String>();
        mapobj.put("phone", "iphone x");
        mapobj.put("imei number", "ankdk9495jfj");
        
        try {
            String jsonoobj = objmapper.writeValueAsString(mapobj);
            System.out.println(jsonoobj);
        } catch (IOException e) {
            
            System.out.println(e);
            
        }
    }
}



objmapper is the object of the ObjectMapper class.

* Map<String, String> mapobj = new HashMap<String, String>() helps us for creating map( ie. key-value pair).We can also use

* HashMap<String, String> mapobj = new HashMap<String, String>() instead of the above one.

Both the above statements are almost the same. Both statements are used for creating a map.

We use mapobj.put(string, string) for entering values to the map.

Further, in the try-catch block, we are using writeValueAsString of ObjectMapper class that creates JSON from java object and returns the generated JSON as a String.

Then we are printing the generated JSON as a String with the help of variable jsonoobj.

Also, read: Java Higher Order Functions

Leave a Reply

Your email address will not be published. Required fields are marked *