How to Convert JSON to a Java Object


If you need to convert JSON to a Java Object, then you can do one of the following:

Option 1 – Using Gson

import com.google.gson.Gson;

public class SimpleTesting {
    public static void main(String[] args) throws InterruptedException {
        String json = """
            {
                "firstName" : "Jane",
                "lastName" : "Doe",
                "dateOfBirth" : "1973-04-29",
                "address" : "81 Hype",
                "city" : "New York",
                "contact" : "0123456789"
            }
            """;
        Student data = new Gson().fromJson(json, Student.class);

        System.out.println(data.getFirstName());
        System.out.println(data.getLastName());
        System.out.println(data.getCity());
    }
}

class Student {
    private String firstName;
    private String lastName;
    private String dateOfBirth;
    private String address;
    private String city;
    private String contact;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
}

Option 2 – Using Jackson

import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class SimpleTesting {
    public static void main(String[] args) throws InterruptedException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        try {
            Student student = mapper.readValue(new File("json-file.json"), Student.class);

            System.out.println("First Name: "+student.getFirstName());
            System.out.println("Last Name: "+student.getLastName());
            System.out.println("City: "+student.getCity());
            System.out.println("Address: "+student.getAddress());
            System.out.println("Contact: "+student.getContact());
            System.out.println("Date of Birth: "+student.getDateOfBirth());
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

class Student {
    private String firstName;
    private String lastName;
    private String dateOfBirth;
    private String address;
    private String city;
    private String contact;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
}