How To Convert Java Object to JSON using Google’s Gson

Connect with

Java to JSON using gson
Java to JSON conversion by using Google’s famous library Gson. There are different libraries available to convert from java object to JSON and JSON to Java Object. The 2 are the most popular first one is Google’s Gson API and the other is Jackson API. In this article, we learn about, how To Convert Java Object to JSON using Google’s Gson.

Note: here order parameter provided more than once, it means, sorting can be on multiple parameters and when you provide the same key multiple times, jersey implementation converts int into a list smartly.

1. Maven dependency for Java to JSON

You can choose the version of the jersey as per your choice or download the Gson jar file from Google and include it in your project lib for converting your custom java Object to JSON.



	com.google.code.gson
	gson
	2.4

2. Custom Java Object with default Gson constructor

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with default Gson constructor.
 * 
 * @return
 */
private static String getJsonString() {
	String jsonData = null;
	try {
		Person person = new Person(1, "ranjeet", new Date());
		Gson gson = new Gson();
		jsonData  = gson.toJson(person);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Json Output:

{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:13:27 AM"}

3. Custom Java Object with GsonBuilder constructor

Google’s GSON API handles the conversions between Java and JSON objects. An instance of this class can be created by invoking the default constructor or using the GsonBuilder class. The GsonBuilder class provides customization, and allows the developer to instantiate Gson as required. Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonStringByGsonBuilder() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		Person person = new Person(1, "ranjeet", new Date());
		
		jsonData  = gson.toJson(person);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Json Output:

{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:13:27 AM"}

4. Embedded Java Object

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonWithEmbededObject() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		Person person = new Person(1, "ranjeet", new Date());
		Map map = new HashMap();
		map.put("name", "ranjeet jha");
		map.put("empid", 345);
		map.put("person", person);
		
		
		jsonData  = gson.toJson(map);
		
		System.out.println("json: " + jsonData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Output:

{"empid":345,"person":{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:15:19 AM"},"name":"ranjeet jha"}

5. List Of Map

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonWithListOfMap() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		List> listMap = new ArrayList>();
		
		Map map = new HashMap();
		map.put("name", "ranjeet jha");
		map.put("empid", 345);

		listMap.add(map);
		
		Map map2 = new HashMap();
		map2.put("name", "Anushka");
		map2.put("empid", 444);
		listMap.add(map2);
		
		jsonData  = gson.toJson(listMap);
		
		System.out.println("json: " + jsonData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Output JSON: :

[{"empid":345,"name":"ranjeet jha"},{"empid":444,"name":"Anushka"}]

6. Download Source Code

Download source code of Gson java to Json Example

7. References

  1. Google Gson API

Connect with

1 thought on “How To Convert Java Object to JSON using Google’s Gson”

  1. self inspiration

    Hey there, You have done a great job. I’ll
    certainly digg it and personally recommend to my friends.
    I am confident they’ll be benefited from this site.

Leave a Comment

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