Convert JSON Into Java Object using JackSon Library

Convert JSON Into Java Object using JackSon Library

Learn to Convert JSON String Into Java Object Using JackSon library. Learn how to convert JSON to Java Object and Java Object to JSON in Java using Jackson API.

Jackson is one of the popular libraries for processing JSON.

We can use Jackson to convert Java objects to JSON string and vice versa.

Maven dependencies for Jackson

For using Jackson, add the following dependency in your pom.xml:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.0</version>
        </dependency>

Parsing JSON String to Java Object using Jackson

The following example illustrates how to parse a JSON into an Employee object.

Employee.java

Employee.java

package com.topjavatutorial.app;

public class Employee {
    private String name;
    private long employeeId;
    private int age;

    public String getName() {
        return name;
    }

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

    public long getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(long employeeId) {
        this.employeeId = employeeId;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

     @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("\n----- Employee Information-----\n");
            sb.append("ID: " + getEmployeeId() + "\n");
            sb.append("Name: " + getName() + "\n");
            sb.append("Age: " + getAge() + "\n");
            sb.append("*****************************");
            return sb.toString();
     }

}

App.java

package com.topjavatutorial.app;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John Doe\",\"employeeId\":\"101\",\"age\":\"25\"}";
        try {
            parseJSON(jsonString);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void parseJSON(String str) throws JsonParseException,
            JsonMappingException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        Employee emp = mapper.readValue(str, Employee.class);
        System.out.println(emp);
    }
}

Output


----- Employee Information-----
ID: 101
Name: John Doe
Age: 25
*****************************

Create JSON String from Java Object using Jackson

The Jackson library can also be used to create JSON from a domain object.

Here is an example :

package com.topjavatutorial.app;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setName("John Doe");
        emp.setAge(25);
        emp.setEmployeeId(101);
        try {
            writeJSON(emp);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void writeJSON(Employee emp) throws JsonGenerationException,
            JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();

        System.out.print("Employee object as JSON = ");
        mapper.writeValue(System.out, emp);
    }
}

Output


Employee object as JSON = {"name":"John Doe","employeeId":101,"age":25}

json java web-development

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Best Web and App Development Company in Ahmedabad

Contact DataPierce for any questions or queries you have about website development, designing, marketing projects or any small or enterprise software development.

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

For World Class Web Development Services in India visit RB Genie

Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...