How to convert Java object to / from JSON (Gson)

By | April 2, 2018

How to convert Java object to / from JSON (Gson) and how to use Gson to convert Java object to / from JSON.

JSON mean is JavaScript Object Notation, it is a lightweight data-interchange format. Java is all about object, often times, you need to convert an object into JSON format for data-interchange or vice verse. For example i will use Gson 2.6.2. You can see many Java applications started to throw away XML format and start using JSON as a new data-interchange format.
But if you want to get better or high performance JSON processor, you can use Jackson 2.

1. Example Convert Java object to JSON

1.1 toJson() – Convert Java object to JSON

	Gson gson = new Gson();
	Staff obj = new Staff();

	// 1. Java object to JSON, and save into a file
	gson.toJson(obj, new FileWriter("D:\\file.json"));
	
	// 2. Java object to JSON, and assign to a String
	String jsonInString = gson.toJson(obj);

 

1.2 fromJson() – Convert JSON to Java object

	Gson gson = new Gson();

	// 1. JSON to Java object, read it from a file.
	Staff staff = gson.fromJson(new FileReader("D:\\file.json"), Staff.class);

	// 2. JSON to Java object, read it from a Json String.
	String jsonInString = "{'name' : 'tiket'}";
	Staff staff = gson.fromJson(jsonInString, Staff.class);
		
	// JSON to JsonElement, convert to String later.
	JsonElement json = gson.fromJson(new FileReader("D:\\file.json"), JsonElement.class);
    String result = gson.toJson(json);

2. Gson Dependency / Gson Java Library

To use Gson, declares the following dependency.

pom.xml
	<dependency>
    	<groupId>com.google.code.gson</groupId>
    	<artifactId>gson</artifactId>
    	<version>2.6.2</version>
	</dependency>

3. POJO

A simple POJO, for testing later.

staff.java
package com.tiket.json;

import java.math.BigDecimal;
import java.util.List;

public class Staff {

	private String name;
	private int age;
	private String position;
	private BigDecimal salary;
	private List<String> skills;

	//...

4. Java Object to JSON

Gson example to convert a Staff object into a JSON formatted string.

GsonExample.java
package com.tiket.json;

import com.google.gson.Gson;

import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class GsonExample {

    public static void main(String[] args) {

        Staff staff = createDummyObject();

        //1. Convert object to JSON string
        Gson gson = new Gson();
        String json = gson.toJson(staff);
        System.out.println(json);

        //2. Convert object to JSON string and save into a file directly
        try (FileWriter writer = new FileWriter("D:\\staff.json")) {

            gson.toJson(staff, writer);

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

    }

    private static Staff createDummyObject() {

        Staff staff = new Staff();

        staff.setName("tiket");
        staff.setAge(35);
        staff.setPosition("Founder");
        staff.setSalary(new BigDecimal("10000"));

        List<String> skills = new ArrayList<>();
        skills.add("java");
        skills.add("python");
        skills.add("shell");

        staff.setSkills(skills);

        return staff;

    }

}

Output

{"name":"tiket","age":35,"position":"Founder","salary":10000,"skills":["java","python","shell"]}

//new file is created in D:\\staff.json"

5. JSON to Java Object

Gson example to read a JSON from a file and convert it back to a Java object.

Gson2Example.java
package com.tiket.json;

import com.google.gson.Gson;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Gson2Example {

    public static void main(String[] args) {

        Gson gson = new Gson();

        try (Reader reader = new FileReader("D:\\staff.json")) {

			// Convert JSON to Java Object
            Staff staff = gson.fromJson(reader, Staff.class);
            System.out.println(staff);

			// Convert JSON to JsonElement, and later to String
            /*JsonElement json = gson.fromJson(reader, JsonElement.class);
            String jsonInString = gson.toJson(json);
            System.out.println(jsonInString);*/

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

    }

}

Output

Staff{name='tiket', age=35, position='Founder', salary=10000, skills=[java, python, shell]}

6. FAQs

Some commonly ask questions.

6.1 Convert a JSON Array to a List, using TypeToken

	String json = "[{\"name\":\"tiket\"}, {\"name\":\"travel\"}]";
	List<Staff> list = gson.fromJson(json, new TypeToken<List<Staff>>(){}.getType());
	list.forEach(x -> System.out.println(x));

6.2 Convert a JSON to a Map

	String json = "{\"name\":\"tiket\", \"age\":33}";
	Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());
	map.forEach((x,y)-> System.out.println("key : " + x + " , value : " + y));

Leave a Reply

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