convert java Map to JSON string

Maven dependency -

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
Or Download - bundle (1.3 MB) 

If you are beginner don't worry learn how to Create new Maven project - In 2 minutes

convert java Map to JSON string - using Jackson Api

package com.ankit.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ConvertJavaMapToJson {

    public static void main(String[] args) {
          try {
                 ObjectMapper objectMappernew ObjectMapper();

                 String jsonInString"";
                
                 List<String> addesssnew ArrayList<>();
                 addesss.add("Paris");
                 addesss.add("London");

                 // Create Map
                 Map<String, Object> mapnew HashMap<String, Object>();
                 map.put("empId", 11);
                 map.put("name""Ankit");
                 map.put("address"addesss);

                 System.out.println("map = "+map);
                
                 // convert java Map to JSON string
                 jsonInStringobjectMapper.writeValueAsString(map);

                 System.out.println("\njsonInString = "+jsonInString);

                 String jsonInStringPrettyobjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);

                 // pretty print the JSON
                 System.out.println("\njsonInStringPretty = "+jsonInStringPretty);


          catch (JsonGenerationException e) {
                 e.printStackTrace();
          catch (JsonMappingException e) {
                 e.printStackTrace();
          catch (IOException e) {
                 e.printStackTrace();
          }
    }

}

//output
/*

map = {empId=11, address=[ParisLondon], name=Ankit}

jsonInString = {"empId":11,"address":["Paris","London"],"name":"Ankit"}

jsonInStringPretty = {
"empId" : 11,
"address" : [ "Paris", "London" ],
"name" : "Ankit"
}

*/


Employee class


import java.util.List;

public class Employee {

    //Fields
    private int empId;
    private String name;
    private List<String> address;
   
    public Employee() {
          super();
    }
   
    public Employee(int empId, String name, List<String> address) {
          super();
          this.empIdempId;
          this.namename;
          this.addressaddress;
    }
   
    @Override
    public String toString() {
          return "Employee [empId="empId", name="name", address="address"]";
    }
   

    //Setter and getters
    public int getEmpId() {
          return empId;
    }
    public void setEmpId(int empId) {
          this.empIdempId;
    }
    public String getName() {
          return name;
    }
    public void setName(String name) {
          this.namename;
    }
    public List<String> getAddress() {
          return address;
    }
    public void setAddress(List<String> address) {
          this.addressaddress;
    }

}


Related links >

1. Jackson JSON -

We can use Jackson api for for processing JSON in java.

Jackson JSON examples


2. Java provides API (JSR 353) for Processing  JSON (JSR 353).
It provides -
  • Object Model API
  • Streaming API

Java API for JSON processing examples >



3. Simple  (simple.JSONObject)



4. Google Gson - processing java json


eEdit
Must read for you :