Convert Java Object to/from JSON using Jackson
This tutorial explains how to convert Java Object to/from JSON using Jackson.
1. Introduction
Jackson is a suite of data-processing tools for Java (and JVM platform), including the flagship JSON parsing and generation library, as well as additional modules to process data encoded in Avro, CBOR, CSV, Smile, XML or YAML
2. Project Setup
Lets create a simple Maven project with the below pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.heapcode</groupId> <artifactId>jackson</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jackson</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.0</version> </dependency> </dependencies> </project> |
In the above pom.xml we added dependencies for Jackson Databind.
3. Java POJO
Lets code two POJO classes Student.java and Department.java, each Student belongs to a particular Department
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.heapcode.jackson.entity; public class Department { private int id; private String name; private String code; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.heapcode.jackson.entity; /** * @author Manjunath */ public class Student { private int id; private String firstName; private String lastName; private Department department; private String regNo; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } 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 Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public String getRegNo() { return regNo; } public void setRegNo(String regNo) { this.regNo = regNo; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public static Student getStudent(){ Department department = new Department(); department.setId(12); department.setCode("CSE"); department.setName("Computer Science & Eng"); Student student = new Student(); student.setDepartment(department); student.setId(1); student.setFirstName("Manjunath"); student.setLastName("Sampath"); student.setRegNo("REG19"); student.setAddress("#12, Dec St, Bangalore"); return student; } } |
4. JSON Convertor Class
Lets us create a convertor class which converts to/from JSON/Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.heapcode.jackson; import java.io.File; import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.heapcode.jackson.entity.Student; public class JacksonJSON{ private static final String FILE_PATH = "/Users/msampath/student.txt"; public static void main( String[] args ) throws JsonGenerationException, JsonMappingException, IOException{ convertToJSON(); convertToObject(); } private static void convertToJSON() throws IOException, JsonGenerationException, JsonMappingException,JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValue(new File(FILE_PATH), Student.getStudent()); System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(Student.getStudent())); } private static void convertToObject() throws JsonParseException, JsonMappingException, IOException{ ObjectMapper objectMapper = new ObjectMapper(); Student student = objectMapper.readValue(new File(FILE_PATH), Student.class); System.out.println("Reading from file..."); System.out.println(student.getId()); } } |
5. Output
6. Project Structure
Download Jackson Example Project
I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.