How to convert map to json in Java

How to convert map to json in Java

Hi,

I my program I have an object of HashMap and I have to output this into json string.

How to convert map to json in Java?

Thanks

View Answers

November 5, 2017 at 3:56 AM

Hi,

You have to use the Google Gson library. Include following code into your pom.xml file:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>

Google Gson library provides Gson class which can be used to format hashmap to JSON string.

It provides the method toJson(map) to converting object into json.

Here is complete example:

package net.roseindia;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MapToJSONExample {

    public static void main(String[] args) {

        Map<String,String> map = new HashMap<String,String>();

        map.put("author", "Deepak Kumar");
        map.put("address", "Delhi, India");
        map.put("web", "https://www.roseindia.net");


        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String jsonString = gson.toJson(map);

        System.out.println("JSON String not formatted:");
        System.out.println(jsonString);


        Gson gsonFormatted = new GsonBuilder().setPrettyPrinting().create();
        String json = gsonFormatted.toJson(map);
        System.out.println("JSON String formatted:");
        System.out.println(json);


    }

}

Thanks


November 5, 2017 at 3:57 AM

If you run the program it will give you output:

JSON String not formatted:

{"address":"Delhi, India","web":"https://www.roseindia.net",

"author":"Deepak Kumar"}

JSON String formatted:

{
  "address": "Delhi, India",
  "web": "https://www.roseindia.net",
  "author": "Deepak Kumar"
}

Thanks









Related Tutorials/Questions & Answers:
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
Advertisements
How to convert hashmap to json in Java?
the instruction in video titled "How to convert hashmap to json in Java?" where you can see the actual running example code. How to convert hashmap to json... hashmap to json in Java?ADS_TO_REPLACE_3 Code for converting the Map into JSON
how to convert json into dataframe in scala
how to convert json into dataframe in scala  Hi, I have a JSON string and I want to convert it to dataframe in scala. how to convert json into dataframe in scala? I am reading some program and creating one line json and now I
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How to convert Python dictionary to JSON?
How to convert Python dictionary to JSON? Python example to create dictionary and convert to JSON pretty print format These days JSON is very popular... with the JSON data format. In this example I will show you how to instantiate Python
How do I convert a dictionary to a JSON object in Python?
How do I convert a dictionary to a JSON object in Python?  Hi, I... is returning the dictionary object. Now our requirement is to convert it to JSON... in the JSON document.ADS_TO_REPLACE_1 I want to the easy way to convert
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output...(); String json = gsonFormatted.toJson(map); System.out.println("JSON String
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output...(); String json = gsonFormatted.toJson(map); System.out.println("JSON String
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
How to convert Collection to Array in Java?
How to convert Collection to Array in Java?  Hello, I have a collection object in data with data in it. How to convert Collection to Array in Java...("This "); Above code create a collection and then add an item to it. Now to convert
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
How to Convert array to list in Java
How to Convert array to list in Java  Hi, I have object of array in Java and I want to convert it to list. How to Convert array to list in Java... ArrayList to list in Java. (adsbygoogle = window.adsbygoogle || []).push
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have java.util.Date object and it has to be converted to date format. How to convert date to string in Java? Thanks   Hi, To convert java.util.Date object
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
How do you map Java Objects with Database tables?
How do you map Java Objects with Database tables?  Hi, How do you map Java Objects with Database tables? thanks
How to convert this Java code into GUI?
How to convert this Java code into GUI?   import java.util.Scanner; public class StudentMarks { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
Convert dictionary to JSON Python
with two key and associated values. Now we want to convert it to JSON string..." } >>> The json.dumps() is used to convert dictionary object to JSON... to convert it to JSON string. If you want to sort the the data in created json
Convert dictionary to JSON Python
() is used to convert dictionary object to JSON string in Python. In this example also we have used josn.dumps() function to convert it to JSON string... to convert dictionary to JSON string using various formatting options
how to convert Visual c++ to java - Java Beginners
how to convert Visual c++ to java  Hi Sakthi here.. How to convert Visual c++ codings to Java codings.. Does both have any similarity in library files
How to hold data in map
How to hold data in map  Holding data in map: i have user1 and he has account1,account2,accoutn3 like that user2,user3 will have accounts. now how to hold users and their accounts in hashmap
How to convert http website into Https from java?
How to convert http website into Https from java?  could any one tell me how to convert http website into Https from java
how to convert time in milliseconds in to hours - Java Beginners
how to convert time in milliseconds in to hours  hello, can anyone tell me how i can convert the time in milliseconds into hours or to display it in seconds in java
how to convert time in milliseconds in to hours - Java Beginners
how to convert time in milliseconds in to hours  hello, can anyone tell me how i can convert the time in milliseconds into hours or to display it in seconds in java
How to convert Arraylist into String Array Java
How to convert Arraylist into String Array Java  Hi, I am beginners of Java programming. Can somebody Suggest me how to convert arraylist to string... information with example. You can find the online example of how to convert arraylist
how to convert java fram to java servlets
how to convert java fram to java servlets   hi every java master or java professional can you teach me how to convert JFram to Java Servlets program hope yours can reply me Thank you. below this is my own coding import
How to Convert PDF into rtf File Java
How to Convert PDF into rtf File Java  Hi, How could in covert the PDF file to rtf file in Java Programming. Please suggest any online example... the online example of Java Convert PDF into rtf File. Thanks
How to convert long to hexadecimal - Java Beginners
How to convert long to hexadecimal  Dear all, anyone know how to convert long data to hexadecimal, please send me the information. Thanks .../java/java-conversion/HexadecimalToBinaryAndLong.shtml Thanks
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
how to convert string to char array in Java
how to convert string to char array in Java  Hi, I have a string in Java which has to be converted to char array. What are the options to convert String object to char Array in Java? how to convert string to char array
How to convert date to UTC format in Java?
How to convert date to UTC format in Java?  Hi, What is UTC Date format and How to convert date to UTC format in Java? I need simple to learn... of world. You can use Java program to convert the date in UTC String
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...); } i want to convert the data from inputStream into Zip Stream and so
how to convert java Applet to Jave Frame
how to convert java Applet to Jave Frame  hi every java master or Java Professional my name is vincent i'm java beginners hope u all can ,tech me how to convert Java Applet to Jave Frame below this code is my applet source code
GUI and how to convert a distance - Java Beginners
GUI and how to convert a distance  i need help.. how to create a GUI application that can be is used to convert a distance unit in miles into its... JLabel("In Inches"); l5=new JLabel("In CM"); b=new JButton("Convert
ModuleNotFoundError: No module named 'convert-csv-to-json'
named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My
ModuleNotFoundError: No module named 'convert-csv-to-json'
named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My

Ads