Thursday , February 22 2018
Home / json / How to convert Java Object to JSON

How to convert Java Object to JSON

Here I am going to show you how to convert a Java object to JSON format using Jackson.

Java object to JSON :

We can convert the Java object to JSON format text using Jackson. To use the Jackson, you need to add the below Jackson jar file to your class path.


Or you can add the below dependency on your project pom.xml file.


Java object to JSON Example:

Here I am going to create a Student Java object and converting to JSON string.

class Student{
    private int studentId;
    private String studentName;
    private String roleNumber;
    private String standard;
    public int getStudentId() {
        return studentId;
    public String getStudentName() {
        return studentName;
    public String getRoleNumber() {
        return roleNumber;
    public String getStandard() {
        return standard;
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    public void setRoleNumber(String roleNumber) {
        this.roleNumber = roleNumber;
    public void setStandard(String standard) {
        this.standard = standard;
    public String toString() {
        return "Student [studentId=" + studentId + ", studentName="
                + studentName + ", roleNumber=" + roleNumber + ", standard="
                + standard + "]";

Converting the Student Java object to JSON string.

package com.onlinetutorialspoint.json;



public class Java_Object_TO_Json {
    public static void main(String[] a){
        Student student = new Student();
        ObjectMapper mapperObj = new ObjectMapper();
        try {
            String jsonFormat = mapperObj.writeValueAsString(student);
        } catch (Exception e) {


Happy Learning  🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


How to convert JSON to Java Object Example

Here I am going to show you how to convert JSON to Java Object using …

Leave a Reply

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