How do I convert JSON into object?

On the previous example, How do I convert object into JSON? we convert object into JSON string. In this example you will see how to do the opposite, converting JSON string back into object.

To convert JSON string to object use Gson.fromJson() method. This method takes the JSON string and the object type of the JSON string to be converted.

package org.kodejava.gson;

import com.google.gson.Gson;
import org.kodejava.gson.support.Student;

public class JSONToStudent {
    public static void main(String[] args) {
        String json = """
                {
                    "name" : "Duke",
                    "address" : "Menlo Park",
                    "dateOfBirth" : "Feb 1, 2000 12:00:00 AM"
                }
                """;

        Gson gson = new Gson();
        Student student = gson.fromJson(json, Student.class);

        System.out.println("s.getName()        = " + student.getName());
        System.out.println("s.getAddress()     = " + student.getAddress());
        System.out.println("s.getDateOfBirth() = " + student.getDateOfBirth());
    }
}

This example will print the following result:

s.getName()        = Duke
s.getAddress()     = Menlo Park
s.getDateOfBirth() = Tue Feb 01 00:00:00 CST 2000

You can find the Student class on the previous example, How do I convert object into JSON?.

Maven Dependencies

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

Maven Central

How do I convert object into JSON?

In this example we are using Google Gson to convert an object (Student object) into JSON notation. Practically we can use this library to convert any object in Java, and it is quite simple. You just need to create an instance of Gson class and then call the toJson() method and pass the object to be converted into JSON string.

package org.kodejava.gson;

import com.google.gson.Gson;
import org.kodejava.gson.support.Student;

import java.util.Calendar;

public class StudentToJson {
    public static void main(String[] args) {
        Calendar dob = Calendar.getInstance();
        dob.set(2000, Calendar.FEBRUARY, 1, 0, 0, 0);
        Student student = new Student("Duke", "Menlo Park", dob.getTime());

        Gson gson = new Gson();
        String json = gson.toJson(student);
        System.out.println("json = " + json);
    }
}

When you run the example above you’ll get an output like:

json = {"name":"Duke","address":"Menlo Park","dateOfBirth":"Feb 1, 2000 12:00:00 AM"}

Below is our Student class.

package org.kodejava.gson.support;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable {
    private String name;
    private String address;
    private Date dateOfBirth;

    public Student() {
    }

    public Student(String name, String address, Date dateOfBirth) {
        this.name = name;
        this.address = address;
        this.dateOfBirth = dateOfBirth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}

Maven Dependencies

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

Maven Central